开发者

Self-Hosting WCF with Self-Hosting WebServer (HTTPListener) on the same port. Possible?

I'm working on an app and I need to provide a web interface to it. I was thinking about using WCF to provide a service for the web interface, and self-host both with my app (no IIS). Now, if those two are not using the same port, the browser will complain about XSS...

Is this possible? Is this a good idea?

EDIT After some investigation, I've managed to make it work.

Here's the webservic开发者_开发百科e self-host code:

var serviceHost = new ServiceHost(typeof(CalculatorService));
serviceHost.AddServiceEndpoint(typeof (ICalculator), new WSHttpBinding(), "http://localhost:8000/webservice");
serviceHost.Open();

Console.WriteLine("CalcService is running.");
Console.WriteLine("Press Enter to terminate the service.");
Console.ReadLine();
serviceHost.Close();

And here's the web host code:

var listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8000/webconsole/");
listener.Start();
Console.WriteLine("listening");
while(true)
{
    HttpListenerContext context = listener.GetContext();
    /* ... */
}

For the webservice to work, I needed to do this


Yep- works fine. HTTP.SYS abstracts the HTTP stuff that WCF uses, and it allows an arbitrary number of processes to share the same port as long as they're all using different path prefixes. I do this all the time for exactly the same reason.

This won't work on XP in IIS 5.1 or with the VS webserver, though- just in case you were going to try. They're not HTTP.SYS based, so they expect to get their ports exclusively. Anything else, though (including XP with 2 WCF hosts), you're good to go.


Sounds fancy, I'll have to watch this one. If nothing else works, you could keep it as two separate ports but then maybe use a reverse proxy to sort out the WCF endpoint?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜