Dot net core: All you can get: Part 2

Saurabh Pati
6 min readJan 26, 2018

--

Launching a dot net core application

The ASP.NET Core app starts as a console application eventually creating a web server. It does sound a bit weird at first but the concept kind of actually grows on you as you go along.

If you create an ASP.NET Core application, you are going to get a ‘Program’ class having a static ‘Main’ method where the web server mentioned above is created.

Program.cs

The code above displays a typical Program class with its Main method. In case if you have not noticed, the word ‘build’ is used quite a few times like in ‘BuildWebHost’, ‘CreateDefaultBuilder’, ‘Build()’. This is because Microsoft loves the ‘Builder Pattern’. If you happen to visit any Microsoft documentation regarding ASP.NET Core, you are sure to find this word. ASP.NET Core uses a builder pattern to build the web host. Allow me to show you a brief diagram I prepared before this term puts you off guard.

Quick Introduction to the Builder Pattern

Lets say there is a manufacturer. A manufacturer eventually wants to ‘build’ some products. It is done by constructing some ‘machines’ that build the products desired by the manufacturer. Here is a quick visual.

Builder Pattern

Now that you have seen both the Program class and the Builder pattern, here is how the builder pattern is used in the Program class.

Builder pattern used in Program class.

The ‘WebHost class constructs a builder which can accept different configurations like which start up class to use i.e. the ‘Startup.cs’ class in this case and after configuring all options that you may have, the ‘Build’ function is called which gives the ‘IWebHost’ instance which in this case is the desired product.

The ‘Run’ function launches the web host and it proceeds to add other configurations in the request processing pipeline.

Inside the CreateDefaultBuilder

Below is the source code for ‘CreateDefaultBuilder’ function which creates the builder, you can view it here too as it is open source. So this function primarily does the following things:

  1. Configures the app to use Kestrel which is a light weight web server.
  2. Configures the current directory to serve as the content root of the application.
  3. Configures the application environment settings using appsettings.json and appsettings.<environment name>.json
  4. Adds any command line arguments if provided.
  5. Configures logging to the console.
  6. Integrates IIS to be the app server for reverse proxying the requests to and fro the Kestrel.
WebHost.cs class’ CreateDefaulter method.

The execution and the purpose of the Main method is completed after this and since the application is configured to use a ‘Startup’ class as you saw above the ‘ConfigureServices’ and ‘Configure’ methods are called one after the other.

Dependency Injection

The ConfigureServices function

Dependency injection was optional in the classic ASP.NET application but it is inbuilt in ASP.NET Core application. The primary purpose of the ‘ConfigureServices’ function is to register services (i.e. classes) that you want to inject as a dependency by using the default IOC container provided by dot net core.

Startup.cs

The ‘IServiceCollection’ is the collection of services that you would want to add in the middle ware. The way to configure your DI container is kind of similar to a unity container as you can see from line 6 to 8. You can also manage the lifetime of the injected dependencies by making them a Singleton, Scoped and Transient.

  1. Singleton dependencies will only be injected once and the same instance will be maintained across the application for the entire lifetime of the application and are only created once per app lifetime.
  2. Scoped dependencies will only be maintained as long as the scope of the dependency is live meaning that they are created once per request.
  3. Transient dependencies will be created at every different reference to the dependency meaning that these are created every time they are requested.

If you want to know about dependency injection in dot net core, I found this to be a very good to do so.

There are some other services that you can add to your service collection like the ‘AddMvc’ function which adds an MVC container to your service and you can make your dot not core application in an MVC pattern.

services.AddMvc();

The Middleware (Request processing pipeline)

The Configure function

The ‘Configure’ function as you can see from the comment in the code above is primarily to customize and configure your request pipeline or middleware. As you can see in the ‘Configure’ function above, you can add different components and configurations like HTTP Strict Transport Security (HSTS), Content Security (CSP), X-Frame-Options (XFO), Authentication and MVC to your request pipeline.

The pipeline

In classical ASP.NET there was a lot of stuff pre-applied in the middleware that you might not have even needed but in ASP.NET Core you can choose what to add in your middleware removing all the unnecessary stuff like in the image above you could only choose to add Authentication, MVC and Static Files for ability to serve up static files like javascript and css in your middleware.

Here is the bigger picture if you want to have a birds eye view at the overall process.

The complete picture of middleware.

Here are some points summarizing the middleware set up shown above

  1. Whenever there is a request originating from the browser it is received by IIS.
  2. The IIS invokes the dotnet runtime on initial start up. This loads the ‘CLR’ which looks for an entry point in the application.
  3. The entry point, if you have not guessed it by now, is found in the App’s ‘Main’ method.
  4. This Main method as you have read at the start of this article creates a web server which is Kestrel in this case.
  5. The request is processed by the middleware and the response is eventually routed back to IIS from Kestrel once it comes back from the middleware. This is then served to the browser by IIS.

This is a departure from the classic ASP.NET which uses the System.Web assembly to establish the interaction to and fro the browser. ASP.NET Core does not have the ‘System.Web’ assembly.

Relevance

Whatever we have learnt above may not be anything practical but if you would like to agree, it is very important to know the environment in which we are likely to be developing in the future. So if you are in the process of choosing or considering ASP.NET Core as the tech stack for an upcoming project, this information will enable you to customize your application with the features provided by ASP.NET Core to develop the application to your needs.

Next Up

In the next article in the series, we are going to deep dive into developing and creating an application in dot net core, understanding the different configurations and options available to us in the process. I hope your download would have completed by now which was recommended in the first article in this series’. I recommend you install it because you are going to need it for the next one.

--

--