Page view counter
How to return html page from WCF with WebHttpBinding Subscribe to this thread
Last post 08-17-2008 8:54 PM by cncolder. 6 replies.
Sort Posts:
08-16-2008 3:07 AM
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/

cncolder

Loading...
Joined on 06-09-2008
Posts 29
08-16-2008 6:34 AM
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.

---------------------------------------------------
MVP Client Application Development
My french website
My french blog

Skyrunner

Loading...
Joined on 12-07-2007
France
Posts 478
08-16-2008 7:12 AM
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/

cncolder

Loading...
Joined on 06-09-2008
Posts 29
08-16-2008 8:12 AM
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.

---------------------------------------------------
MVP Client Application Development
My french website
My french blog

Skyrunner

Loading...
Joined on 12-07-2007
France
Posts 478
08-16-2008 12:07 PM
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

Loading...
Joined on 08-16-2008
Posts 10
08-16-2008 3:43 PM
Marked as Answer
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();

        }
    }

rico.sauve

Loading...
Joined on 08-16-2008
Posts 10
08-17-2008 8:54 PM
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/

cncolder

Loading...
Joined on 06-09-2008
Posts 29
Microsoft Communities