Can I serve up web pages from a WCF Service NOT hosted in IIS?
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开发者_C百科 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.
This is the top link when searching for "self hosted wcf webpage", so I though I'd add a more complete answer. The answer by @Jared-G was close, but I wanted to host an Angular application as a frontend for my WCF service. This meant being able to serve arbitrary js/css/img files as well. An extension of the previous answer:
[ServiceContract]
public interface ITestService
{
// Webpage hosted at {Service URL}/Client
[OperationContract]
[WebGet(UriTemplate = "Client/{*uri}")]
Stream GetPage();
}
[ServiceBehavior]
public class TestService : ITestService
{
public Stream GetPage(string uri)
{
if (uri == "") uri = "test.htm"; // Default page, if root path specified
var basePagePath = "Client\\"; // Default path for webpage, relative to executable directory.
var pageStream = new FileStream(Path.Combine(Environment.CurrentDirectory, basePagePath, uri), FileMode.Open, FileAccess.Read, FileShare.Read);
var context = WebOperationContext.Current;
if (context != null)
{
context.OutgoingResponse.ContentType = "text/html";
}
return pageStream;
}
}
The gotchas of self-hosting instead of IIS generally consist of loss of instrumentation/recycling/recovery capabilities of IIS' app pools. However, self-hosted tends to perform substantially better.
Yes, this is possible. You could host the WCF service in a .NET application, or Windows Service on an HTTP or TCP port.
You would then have to host your site in another web server, and create a reference to your WCF service in your web application.
精彩评论