Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I serve up web pages from a WCF Service NOT hosted in IIS?

Tags:

wcf

I have a WCF Service that will be used like a standard web service, but I also want to provide a configuration UI for various service settings. Is it possible to serve up standard HTML pages when the service is not being hosted in IIS? If so, what "gotchas" are there?

like image 362
Mark Carpenter Avatar asked Dec 04 '22 22:12

Mark Carpenter


2 Answers

Yes you can. This is exactly how the WSDL help pages are served when the add the HTTP Help behavior to your service.

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "test")]
    Stream GetPage();
}

[ServiceBehavior]
public class TestService : ITestService
{
    public Stream GetPage()
    {
        var pageStream = new FileStream(Path.Combine(Environment.CurrentDirectory, "test.htm"), FileMode.Open, FileAccess.Read, FileShare.Read);
        var context = WebOperationContext.Current;

        if (context != null)
        {
            context.OutgoingResponse.ContentType = "text/html";
        }

        return pageStream;
    }
}

You can also build in the page in a memory stream. Additionally, in WCF 4.5+, the HttpResponseMessage type is provided for serving html pages. Hope this helps!

like image 73
Jared G Avatar answered May 28 '23 05:05

Jared G


WCF is API used for creating services. Handling "configuration web pages" is out of scope of this API. If you really want to do something like that it means that you must create another REST (webHttp) service which will expose operations serving your web pages and accept HTTP POSTs from your web pages. It is possible but it is a lot of work to do because current WCF version doesn't like content type of POSTed HTML forms (application/x-www-form-urlencoded and multipart/form-data). You can in the same way implement your own self hosted "web server" by using HttpListener.

like image 32
Ladislav Mrnka Avatar answered May 28 '23 06:05

Ladislav Mrnka