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

StackOverflow https://stackoverflow.com/questions/1732115

  •  20-09-2019
  •  | 
  •  

Question

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 webservice 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

Was it helpful?

Solution

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.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top