ASP.NET Core 2 MVC.
Microsift.AspNet.Hosting.IWebHost
interface contains the Start()
method. Also, the Microsoft.AspNetCore.Hosting.WebHostExtensions
class defines the Run()
extension method for the IWebHost
interface.
The Run()
method runs a web application and block the calling thread until host shutdown.
At the same time the Start()
method doesn't block the calling thread until host shutdown. At this case the browser closes before it can show information to user.
Hmm... When it can be usefull to use the IWebHost.Start()
method?
At run time, the ConfigureServices method is called before the Configure method. This is to register custom service with the IoC container which can be used in Configure method.
The host provides services that are available to the Startup class constructor. The app adds additional services via ConfigureServices . Both the host and app services are available in Configure and throughout the app.
What is a Host and what's the importance of Host in ASP.NET Core application? The host is responsible for app startup and lifetime management. At a minimum, the host configures a server and a request processing pipeline. The host can also set up logging, dependency injection, and configuration.
Here, two servers are running. One is IIS and another is Kestrel. This model is a default model for all the applications implemented before . NET Core 2.2.
Not all hosting is performed in a classic serving-pages-over-the-internet scenario. For example, you may want to serve content from your WPF app or a Windows service. In this situation you probably don't want the call to block - your app will have other things to do. For example, lets say you have a WPF app and you want to service content from it, you could simply extend the main
method:
private IWebHost _webHost;
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Create the host
_webHost = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
//We want to start, not run because we need the rest of the app to run
_webHost.Start();
//Run the app as normal
Application.Run(new MainForm());
//We're back from the app now, we can stop the host
//...
}
This is useful when you are testing your web service in the same process that is running the test suite.
For an example, you don't have to look any further than Microsoft's TestServer
implementation. Within its constructor, it is calling IWebHost.StartAsync()
instead of Run()
. This allows the IWebHost
to run on a non-blocking thread while your test suite runs requests against it, disposing of the TestServer
when the test suite completes.
This may also be called explicitly for end-to-end tests where your service is getting requests indirectly from another service. For example, I have a service that is pushed messages from Google Cloud PubSub. So in my test suite I call Start()
on my service's encapsulating IWebHost
, send a message to the pubsub emulator running in a docker container, and that calls my test host. I verify the test host received requests as I expected, then I shut down the test host.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With