SSL Port Sharing (OWIN vs Web API)

Feb 4, 2016 at 11:06 PM
Edited Feb 4, 2016 at 11:07 PM
Hi,

I have couple of services on WCF using a port with HTTPS. The addresses are as follows

https://localhost:356/Service1 https://localhost:356/Service2 I want to self host a Web API based service on the same port.
WIth plain old Web API self host (without OWIN) - I am able to create the service and use HTTPS to communincate.

However, with OWIN Katana - I am not able to do the same. I run into the following exception.

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.HttpListenerException:
Failed to listen on prefix 'https://localhost:356/WebService/' because it conflicts with an existing registration on the machine.
at System.Net.HttpListener.AddAllPrefixes()
at System.Net.HttpListener.Start()
at Microsoft.Owin.Host.HttpListener.OwinHttpListener.Start(HttpListener listener, Func2 appFunc, IList1 addresses, IDictionary2 capabilities, Func2 loggerFactory)
at Microsoft.Owin.Host.HttpListener.OwinServerFactory.Create(Func2 app, IDictionary2 properties)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuilder builder)
at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(StartContext context)
at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options)
at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options)
at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options)
at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
at Microsoft.Owin.Hosting.WebApp.Start[TStartup](String url)
at TestHost.Program.RunOwinServer()
at TestHost.Program.Main(String[] args)

I do not see a reason why Web API Self Host can work and OWIN Self Host cannot. Is this a limitation of using HttpListener?
Coordinator
Feb 5, 2016 at 4:34 AM
The WebApi self-host is built directly on WCF so it makes sense that they work together. I suspect there's something in the way WCF registers with netsh/http.sys that prevents sharing with non-wcf servers.
Feb 5, 2016 at 5:38 PM
Looks like this is a limitation of HttpListener as it tries to take complete control of the port and does not allow port sharing.
I wanted to use different authorization middleware and looks like I have to stick with Web API self host because of this limitation.
Coordinator
Feb 5, 2016 at 5:43 PM
Edited Feb 5, 2016 at 5:43 PM
No, HttpListener does allow port sharing. Try starting multiple HttpListener instances, or IIS and HttpListener. It's even what WCF uses internally. I think it's just the way WCF registers the port that it locks out others.
Apr 15, 2016 at 8:50 PM