Lee Grissom
Lee Grissom

Reputation: 9953

ASP.NET Web API Self-Host both HTTPS and HTTP in single service?

I have a public self-hosted ASP.NET Web API service that exposes two controllers. One of them I would like to have accessible only via HTTPS, and the other I don't. Can this be done within a single service? If so, can you provide a few hints? It seems like I would need to register two base addresses, but I don't see how that's possible for a single service.

Upvotes: 6

Views: 3436

Answers (2)

Dror Harari
Dror Harari

Reputation: 3307

Because HttpListenerBase (of ServiceStack.Host.HttpListener) does not override its Listener if not-null when starting then the following could be done in your host class which inherits from HttpListenerBase:

public override ServiceStackHost Start(string urlBaseNoPrefix)
{
    // Here urlBaseNoPrefix is something like "web.acme.com/app/" 
    Listener = new System.Net.HttpListener();
    Listener.Prefixes.Add("http://" + urlBaseNoPrefix);
    base.Start("https://" + urlBaseNoPrefix);
    return this;
}

Upvotes: 0

Darrel Miller
Darrel Miller

Reputation: 142184

You need to create two HttpServer instances, one for http and one for https. I've been trying to find out why this limitation exists because I know HttpListener can handle registering both for the same listener.

Anyway, if creating two HttpServer instances really doesn't work for you then you will need to look at the Katana project and the Microsoft.Owin.HttpListener. That does support multiple addresses but unfortunately the default Katana startup code doesn't! But I have it on good authority there is a way to customize the HttpListener on startup to make it possible.

Upvotes: 7

Related Questions