Skip to main content

Microsoft Silverlight

Answered Question How to return html page from WCF with WebHttpBindingRSS Feed

(0)

cncolder
cncolder

Member

Member

45 points

29 Posts

How to return html page from WCF with WebHttpBinding

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?




-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.ViTarn.com/

Skyrunner
Skyrunner

Contributor

Contributor

2489 points

485 Posts

Silverlight MVP

Re: How to return html page from WCF with WebHttpBinding

You can return what you want.

You can return HTML by string or by Stream as you want.

cncolder
cncolder

Member

Member

45 points

29 Posts

Re: How to return html page from WCF with WebHttpBinding

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.




-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.ViTarn.com/

Skyrunner
Skyrunner

Contributor

Contributor

2489 points

485 Posts

Silverlight MVP

Re: How to return html page from WCF with WebHttpBinding

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
rico.sauve

Member

Member

15 points

15 Posts

Re: Re: How to return html page from WCF with WebHttpBinding

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 

rico.sauve
rico.sauve

Member

Member

15 points

15 Posts

Answered Question

Re: Re: Re: How to return html page from WCF with WebHttpBinding

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();

        }
    }

cncolder
cncolder

Member

Member

45 points

29 Posts

Re: Re: Re: How to return html page from WCF with WebHttpBinding

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. Yes 




-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.ViTarn.com/
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities