concept WebHost in category c#

appears as: WebHost, WebHost, A WebHost
Code like a Pro in C#

This is an excerpt from Manning's book Code like a Pro in C#.

We need some way to configure our brand-new .NET Core app on launch to be able to route to endpoints and accept HTTP requests. We have to finish the configuration and tie it to the WebHost running our application. WebHost is the underlying process that runs the web service and interacts with the CLR. Our application lives inside the WebHost, which in turn lives inside the CLR. We can draw similarities between web containers (such as IIS) and Tomcat. To put it in Java terms, .NET Core is your JVM and Spring while WebHost is your Tomcat. Often, we also use ASP.NET in our web services. This launches a “host” that is responsible for app startup and lifetime management. At a minimum, the host configures the server and request processing pipeline.

Figure 5.6 A Web Service runs inside the WebHost, which runs inside the CLR. This model allows the CLR to spin up a WebHost that can execute our Web Service.

There is a defined way of configuring the WebHost in .NET Core. To create and configure our WebHost, we need to follow three steps:

  • Set the startup URL to be http://0.0.0.0:8080
  • Build an instance of WebHost using WebHostBuilder.
  • Figure 5.8 In this section we create an instance of WebHostBuilder, set a startup URL, and build the WebHost instance. This code uses the builder pattern, explained in section 5.2.2.

    In Program’s Main method, we add a call to WebHost.CreateDefaultBuilder. This call returns a WebHostBuilder, with some defaults already set, passing in the console arguments to the constructor. We chain a call to UseUrls to specify the URL and port we want to serve on is. Then we call Build to build and return the actual WebHost. We assign the output to a variable of type IWebHost. We assign our new WebHost to an explicitly typed variable of type IWebHost (the return type of the WebHostBuilder, there is polymorphism at play here!). Finally, we run the WebHost by calling webHost.Run(). To use all these classes and methods, we need to use the System, Microsoft.AspNetCore, and Microsoft.AspNetCore.Hosting namespaces.

    using System;
    using Microsoft.AspNetCore; 
    using Microsoft.AspNetCore.Hosting;
     
    namespace FlyingDutchmanAirlines
    {
        class Program
        {
            static void Main(string[] args)
            {
                IWebHost webHost = ➥WebHost.CreateDefaultBuilder(args).UseUrls("http://0.0.0.0:8080").Build➥();
            webHost.Run();
            }
        }
    }

    If you try to compile and run the service in this state, the service launches but then terminates with an InvalidOperationException. This exception tells us we do not have a Startup class configured and tied to the WebHost, and that is correct! But before we create this Startup class, let’s leave the Program class in the best shape possible. We have our WebHost creation and call to Run in the Main method, but should it really be in there?

    In section 1.4 we discussed the need for methods that read like a narrative. If I am a new developer, looking at a public method (in this case Main), I probably do not care about implementation details. Instead, I want to get an understanding of the broad strokes of the operations and concepts dealt with in the method. To that end, we can extract the initialization and assignment of WebHost, and the call to webHost.Run, into a separate, private method:

    Note that the exact constraint names may differ on your system, as they are auto-generated. Perhaps the most notable thing here is the calls to modelBuilder. The OnModelCreating method sets up the entities internally for Entity Framework Core, along with the required key constraints. This allows us to operate on the entities without directly messing with the database. There also is a generated method (and call to) called OnModelCreatingPartial. The Entity Framework Core console toolset generated the OnModelCreatingPartial method so you can execute additional logic as part of the model creation process. We are not going to do that, so we can remove the OnModelCreatingPartial method and the call to it.