govin
govin

Reputation: 6693

Web API self host - bind on all network interfaces

How do you make a Web API self host bind on all network interfaces?

I have the below code currently. Unfortunately, it binds only on localhost. So access to this server from other than localhost is failing.

var baseAddress = string.Format("http://localhost:9000/"); 
            using (WebApp.Start<Startup> (baseAddress)) 
            {
                Console.WriteLine("Server started");
                Thread.Sleep(1000000);
            }

Upvotes: 34

Views: 15094

Answers (2)

Daniel M&#252;ller
Daniel M&#252;ller

Reputation: 453

If you get access exceptions, please DO NOT start Visual Studio as admin user. Add an URL reservation instead. The following example assumes that you want to open port 9000 as HTTP service on all ports & hostnames (http://+:9000/) without any user restriction.

Start a command console window as administrator and execute:

netsh
netsh> http add urlacl url="http://+:9000/" sddl=D:(A;;GX;;;S-1-1-0)

The SDDL translates to "all users" from your current domain / machine.

Modify your code accordingly:

var baseAddress = "http://+:9000/";
using (WebApp.Start<Startup> (baseAddress)) 
{
  // your code here
}

You can delete the reservation by running:

netsh
netsh> http delete urlacl url="http://+:9000/"

However, Microsoft recommends to avoid Top-level wildcard bindings, see:

For more information about the difference between http://*:9000/ and http://+:9000/ see:

Upvotes: 12

evilpilaf
evilpilaf

Reputation: 2030

Just change the base address like this

        var baseAddress = "http://*:9000/"; 
        using (WebApp.Start<Startup> (baseAddress)) 
        {
            Console.WriteLine("Server started");
            Thread.Sleep(1000000);
        }

And it should bind correctlly to all interfaces.

Upvotes: 48

Related Questions