This video is available to students only

Adding New Dotnet 6 Features to Our Application

In this lesson, we're going to add .net6 features to our application

Adding new dotnet 6 features to our application#

In this lesson, we will look at some new features of dotnet 6. I have created a new project using version 6 and if you notice, we don't have startup class anymore. It's not that we can't use startup class; it's just that version 6 doesn't come with it anymore. This is where the new hosting model comes into the picture. This is a minimal hosting model which requires only one file now. This is a positive step, because previously, we had a lot of code for just writing "Hello World" in a console. Now we have to write only 4 lines to do so. For bigger projects, we have to write more code of course, but we won't need the startup class for that in dotnet 6.

if we open the program file in our existing project, we have a main method and CreateHostBuilder method which calls our startup class. If we open the program class inside the dotnet 6 version, we have a lot of code missing; it doesn't even have the main method. It's not going to change the functionality in any way. It's just an attempt to get rid of the boilerplate code. In version 6, we have a builder class and below that, we have the dependency injection container which is equivalent to the startup class, configure services method. So it's doing the same job; but instead of putting it inside this configure services method and passing the services as a parameter, we have services available from our create builder method as arguments.

Below this, we are building the app and we have the middleware pipeline for our http request. This is what we were doing inside the startup class, configure method. And what we also have is not using statements inside our new program class. This is what Implicit using feature is doing, if we go inside csproj file of version 6, it has ImplicitUsings enabled. Although it's not visible inside our program class; however, it exists in the background.

Microsoft has implicitly added this feature because one of the new features in Dot 6 is the concept of global using statements. With this feature, instead of having multiple using statements in every single file, we can have a global using statements file which will have the common using statements across multiple classes. In short, we will have a single file which will take care of the imports. Version 6 also comes with nullable property; which means we will have to explicitly define a property and make it optional so that it can be null. What we can do is that we can re-configure our project so that it can take advantage of the features provided by version 6.

So let's open our project and take a look at what we need to do. Let's start with the program file; we can comment out everything from our namespace till the end, as we are going to start from scratch. Let's now refactor our program class with minimum hosting model. Let's start with creating a new variable, builder; same as we saw inside the dotnet 6 version. This will be equal to WebApplication.CreateBuilder where we can pass the arguments. Let's import it from Microsoft.AspNetCore.Builder. After this, we will add the services container which is equivalent to the ConfigureServices method inside version 5. So we can leave a comment which says add services. Below this, we can copy everything we had inside startup class. I will select services and replace it with builder.Services; we still need to import these so let's do it. Everything is good apart from the config, so for this, we can use builder to get the config. So instead of config, we can write builder configuration and we can import from Microsoft.Extensions.Configuration. Let's do the same for other config values as well. We have to do it this way because now we are not creating a constructor so we can't really inject the configuration into it like before.

Start a new discussion. All notification go to the author.