3
votes

I am not sure if I am about to ask a noddy question, but here it goes. I have a WCF service hosted in a Windows service and a new requirement has come in: A keepalive web page necessary for load balancing. Is it possible to host this page from my Windows service?

Do I need to resort to hosting the WCF service in IIS? I would prefer not to do this.

Thank you.

3
Hi, A web page needs to run on a web server, either you use IIS or IIS Express or you develop your own or you use a third party one. - Davide Piras
Okay so my only option is to host the WCF service in IIS? - youwhut
why does it have to be a website? A person has to be able to access it? - Marcom
It is required by the load balancer to retain the server in the cluster. - youwhut
@David Piras: It is absolutely possible, though maybe architecturally not advisable, to host a "web page" inside a "pure" WCF service. Use the "webHttpBinding" for the endpoint, have a method/operation contract returning a "System.IO.Stream" and annotate it with "[WebGet]" or "[WebInvoke]", set the "WebOperationContext.Current.OutgoingResponse.ContentType" to "text/html" and render/write the webpage to the stream returned - getting as fancy as you want. You can even embbed links back to your own service (referencing the URLs of (other) operation contracts with WebGet/WebInvoke). - Christian.K

3 Answers

2
votes

Try to create a service contract to handle the requests of the loadbalancer exposed using a basicHttpBinding

1
votes

Your windows service may expose an HTTP endpoint by basicHttpBinding or wsHttpBinding. IIS is not required. To host an HTTP endpoint via IIS has some benefits though like message based activation.

And if IIS is already running on the same machine be sure to pick a port different from 80 for your windows service HTTP endpoint since IIS will listen to requests on port 80 and then the requests wouldn't reach your service.

1
votes

More than one year later, I hope this can help

http://msdn.microsoft.com/en-us/library/bb412178.aspx

How to: Create a Basic WCF Web HTTP Service

Look at the contract:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet]
    string EchoWithGet(string s);

    [OperationContract]
    [WebInvoke]
    string EchoWithPost(string s);
}

you have the request (or post request) in 's' param and you must write the output html in the return. That's all