2
votes

I am using VS 2015 Update3. I have ASP.NET core web api with the following Program.cs code

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()       
            .UseUrls("http://*:5000")         
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

Then in VS I have configured IIS Express to run on port 40000

This is how launchSettings.json looks like

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:40000/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/workunit",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Api": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000/api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Then I press F5 to run app using Visual Studio. When i make a request to API i get the result back so everything works fine.
In Program.cs I have .UseIISIntegration(). So I was under impression when request comes to IIS express, it just forwards that request to Kestrel. So I thought both IIS Express and Kestrel has to run at the same time on 2 different ports.

However when I ran netstat -ab command I noticed IIS express is running on port 40000 as expected but nothing is running on port 5000. In-fact just to test I changed IIS Express port also to 5000 and it worked fine. I was expecting conflict between IIS Express and Kestrel, but that didn't happen.

So question is does VS 2015 uses Kestrel at all?

1

1 Answers

2
votes

VisualStudio does use Kestrel. The trick is that when Kestrel is used behind IIS/IISExpress it is not using the port you specified. Rather, IIS chooses a random port which it will use for communication and this is the port Kestrel has to be using (the port is overriden in the UseIISIntegration). If you are running Kestrel directly .UseIISIntegration is just a no-op and therefore Kestrel will listen on the port you specified in UseUrls. One of consequences of this is that the order of .UseUrls and .UseIISIntegration is important - if you put .UseUrls after .UseIISIntegration you would always override the port and as a result you would not be able to start your application with IIS. If you want to exactly what is happening take a look at a blog post I wrote on this very subject.