views:

516

answers:

2

Hi,

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

A: 

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?

roufamatic
+1  A: 

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.

nitzmahone
Do you have an example of that? I tried something but it didn't work. Whatever was started second (wcf or webserver) was complaining...
Subb
What webserver are you using? It has to be based on HTTP.SYS (IIS6+ or HttpListener, etc), and your service has to be configured with a URI path that IIS hasn't grabbed (ie, you can't listen on /, it'll have to be /Services/Foo or whatever).
nitzmahone
Got it working. I'll add the code to the main post.
Subb