Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to return html page from WCF with WebHttpBinding
6 replies. Latest Post by cncolder on August 17, 2008.
(0)
cncolder
Member
45 points
29 Posts
08-16-2008 3:07 AM |
Because I am using Self-Host WCF Service. So I return clientaccesspolicy.xml to my Silverlight client. It works well.
And then, I thinks, if I can return the other type contents. eg. html page, jpg picture.
If I can do that. I will create webservice & webserver in a same application without the other Server Software.
Looks cool~ But, is there any way? or no way?
Skyrunner
Contributor
2489 points
484 Posts
08-16-2008 6:34 AM |
You can return what you want.
You can return HTML by string or by Stream as you want.
08-16-2008 7:12 AM |
My mean is that:
Return the html file not a xml string. How to avoid XmlSerializer? If not, the browser cannot show it like this:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><html><head><title>HTML页</title></head><body><h3>页面内容</h3></body></html></string>
I wanna let WCF have the behavior of web server.
08-16-2008 8:12 AM |
What's the matter ?
WCF Method
public string GetHtml() { System.Net.WebClient client = new System.Net.WebClient(); return client.DownloadString(new Uri("http://broux.developpez.com/")); }
And when I call it from a Silverlight app, I receive just the HTML, nothing else.
rico.sauve
15 points
15 Posts
08-16-2008 12:07 PM |
Hmm. I interpretted your question differently. Are you asking how to make your self-hosted WCF Service behavior more like typical Web Server. If so, the article might help.
http://msdn.microsoft.com/en-us/magazine/cc135976.aspx
08-16-2008 3:43 PM |
Actually, this may be a better example: http://msdn.microsoft.com/en-us/library/cc681221.aspx
I've modified the msdn example above to support .html and .xap files:
With this your Silverlight App can be accessed via a URL such as: http://localhost:8000/Service/Files/LoadXap.html
HTML snippet:
<div id="silverlightControlHost"> <object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%"> <param name="source" value="YourSilverlightAppHere.xap"/> <param name="onerror" value="onSilverlightError" /> <param name="background" value="white" /> </object> </div>
WCF Code:
// define the service contract [ServiceContract] public interface IFileHost { [OperationContract, WebGet(UriTemplate = "Files/{filename}")] Stream Files(string filename); } // implement the service contract public class Service : IFileHost { public Stream Files(string filename) { Stream stream = (Stream)new FileStream(filename, FileMode.Open); //Set the correct context type for the file requested. int extIndex = filename.LastIndexOf("."); string extension = filename.Substring(extIndex, filename.Length - extIndex); switch(extension) { case ".html": case ".htm": WebOperationContext.Current.OutgoingResponse.ContentType = "text/html"; break; case ".xap": WebOperationContext.Current.OutgoingResponse.ContentType = "application/x-silverlight-2-b2"; break; default: throw(new ApplicationException("File type not supported")); } return stream; } } class Program { static void Main(string[] args) { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); host.AddServiceEndpoint(typeof(IFileHost), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); host.Open(); Console.WriteLine("Service is running"); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } }
08-17-2008 8:54 PM |
Cool~ Thank you very much Rico!
I think hard and try that for one week. But no one can return the right result to Browser. Even the FileStream... I miss the right way only one step...
Thanks again.