In this section I discuss how to run the ASP.NET Core application using IISExpress or Kestrel, both from the command line and using Visual Studio. I also change the default port used by Kestrel when running from the command line.
- [Narrator] So let's talk about how you run ASP.NET Core applications. As mentioned earlier, an ASP.NET Core application is simply a console app that creates a web server. The web server is created by a WebHostBuilder. And the WebHostBuilder requires you to opt-in to the features that you would like supported. This is vastly different than prior versions where everything was included upfront and then you had to opt-out.
ASP.NET Core applications can run on Kestrel or IIS or IIS Express as we are doing here in development. And Kestrel is a cross-platform web server based on libuv, and it's included by default in all ASP.NET Core projects. So you can run the application as you would with traditional ASP.NET applications by hitting F5 or Control + F5 in Visual Studio. But you also have the option of running under Kestrel in Visual Studio.
And those profiles are configured in launchSettings.json as we discussed in the last section. You can also run from the command line by just typing dotnet run. Now to do this, you have to be in the same directory as the csproj for your ASP.NET Core application, and we will see an example of how to do this shortly. So here in Visual Studio, we can run under IIS Express which is currently selected or running Kestrel using the profile SpyStoreAPI.
So if we run this now, it launches a browser and goes to port 60910 as we saw in the launchSettings file. If we go back to Visual Studio, hit Stop, let's switch over to Kestrel and hit Run, it will again launch a browser going to our default values controller. And we see it launch on port 60911.
When you run through Kestrel under Visual Studio, it launches a command window in the background since it is a command line option that's actually being run. And I'm showing that command window here, showing that it's listening on port 60911, and press Control + C to shut down. So we'll go ahead and shut it down. You see that it stops Visual Studio. So now I've opened the command window in the same directory as my project, and I'm going to type dotnet run.
It will again fire up Kestrel, run the application, but now it's listening on port 5000. So why the change? When I'm running in Visual Studio, everything is controlled by the launchSettings.json file. When I'm running outside of Visual Studio, it is running outside of the launchSettings.json settings and Kestrel defaults to port 5000. It's also looking at my machine's environment variables and determining that it is not in development because that environment variable doesn't exist, so I don't get the additional logging that you saw when we ran from within Visual Studio.
So we want to fix that. And it's very easy to do. We open up Program.cs. And here is the WebHostBuilder. It says UseKestrel, set the ContentRoot, use IIS as well, use the Startup file named Startup.cs, and we'll cover that shortly, use ApplicationInsights, and then build and run. We want to update Kestrel to use the port that we have set in the launchSettings.json.
And this does a couple things. First of all, that makes it consistent whether running from the command line or from within Visual Studio. And we also want to move away from port 5000 because if you're working with one or more sites then you would have port conflicts. So we're just going to simply put in http, anyhost 60911, save that. Let's go back to our command prompt, and we'll do dotnet run again.
And you see that we're now listening on port 60911. So go ahead and cancel out of that. One more thing we want to do is we want to make sure that we are running in development when we run from the command line. And so, we can type in set aspnetcore_environment =development.
And now, we type dotnet run, not only are we running on port 60911, but you see that our hosting environment has been changed over to development. So let's put that in a batch file so we can run it multiple times without having to type that each time. So let's exit out of here. So right here in our main project window, New Text Document, and we'll call it run_development.cmd.
And yes, I want to change the extension. Then let's edit. We'll just pull up Notepad to be nice and simple. And we'll say set aspnetcore_environment =development. Let's do a dotnet restore, a dotnet build so it's going to restore all the packages, build the project, and then finally, a dotnet run.
That's going to run the application. But since it's a batch file, we don't want it to close so we can actually Control + C out of Kestrel, so we'll put in a pause. So we'll save that, close it. And when we run it, it sets the environment, pulls down the packages if needed, does a build. When it runs, we see right there it is in development and it's running on port 60911.
Go ahead and Control + C to get out. And we've shut down the server. So those are the different ways that you can run an ASP.NET Core application.
- Creating the .NET Core project
- Adding Entity Framework
- Migrating the data access layer (DAL)
- Configuring services and the HTTP pipeline
- Adding remaining services to the dependency injection container
- Migrating controllers and actions
- Testing the services