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?
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With