Adding New Dotnet 6 Features to Our Application

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

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:06] In this lesson, we will look at some new features of .NET 6. I have created a new project using version 6.

    [00:07 - 00:12] And if you notice, we don't have the startup class anymore. It's not that we can't use startup class.

    [00:13 - 00:18] Version 6 just doesn't come with it anymore. This is where the new hosting model comes into the picture.

    [00:19 - 00:28] 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.

    [00:29 - 00:37] Now we have to write only these four lines. For bigger projects, we have to write more code, of course, but we don't need the startup class for that in .NET 6.

    [00:38 - 00:55] Now if we go back to our project and if we open the program.cs class, we have a main method and a create host builder method, which calls our startup class. And if you open the program class inside .NET 6 version, let me open that.

    [00:56 - 01:00] We have a lot of code missing. It doesn't even have the main method.

    [01:01 - 01:08] Although 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.

    [01:09 - 01:30] 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 the configure services method and passing the services as a parameter, we have the services available from our create builder method as arguments below this.

    [01:31 - 01:40] We are building the app and we have the middle web pipeline for our HTTP request. This is what we were doing inside the startup class configure method.

    [01:41 - 01:48] And what we also have is no using statements on top. This is what the implicit feature is doing.

    [01:49 - 01:59] If you open the csproch file of version six, it has implicit usings enabled. So in our program file, we don't see the using import, but in the background, they are located.

    [02:00 - 02:19] Microsoft has implicitly added this feature because one of the new features in .NET 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.

    [02:20 - 02:34] In short, we will have a single file, which will take care of the imports. Version six also comes with the nullable property, which means we will have to explicitly define a property and make it optional so that it can be null.

    [02:35 - 02:46] Now what we can do is we can reconfigure our project so that it can take advantage of the features provided by version six. So let's close this and let's take a look what we need to do.

    [02:47 - 02:57] Let's close everything else and let's start with the program.cs file. Let's comment out everything from namespace API till the end.

    [02:58 - 03:03] Let's start from scratch. Let's now refactor our program class with minimum hosting model.

    [03:04 - 03:25] Let's start with creating a new variable called build up. So here, let's create a variable called build up and this will be equal to web application .createBuilder and we can pass the arguments.

    [03:26 - 03:34] Now, let's import web application using Microsoft ASP net core build up. This is the same as the version six.

    [03:35 - 03:52] After this, we will add the services container, which will be equivalent to the configure services method inside version five. So we can leave a comment, which will say add services and below this, we can copy everything we had inside our startup class.

    [03:53 - 04:07] So let's open our startup class and let's copy everything we had inside the configure services method. So let's start from here and we will stop actually here.

    [04:08 - 04:23] Now let's go back to our program class and paste this code here. I will select services.

    [04:24 - 04:39] Now I can replace it with builder.services with s capital. We still need to import these.

    [04:40 - 05:14] Everything is good apart from this. So let's go back to our program class.

    [05:15 - 05:33] So let's go back to our program class. Now we can replace it with builder.configuration and this will take care of all the errors.

    [05:34 - 05:43] 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.

    [05:44 - 05:51] Now by doing this, we have taken care of the configure services method. After this, we can configure the HTTP request pipeline.

    [05:52 - 06:13] So when this is ending, we can create another comment and we can see HTTP request pipeline. Now we can go back to our startup class and copy everything from our configure method.

    [06:14 - 06:37] So let's start copying it and let's go back to our program class and paste everything here. Now on top of it, we will create a new variable called app, which will be equal to builder.build.

    [06:38 - 06:44] Now let's start importing. We are just left with the E and V and now we can use app.environment.

    [06:45 - 07:01] Actually, we don't need this app.use endpoints anymore. So I can get rid of this and this and instead of endpoints, I can use app.map controllers.

    [07:02 - 07:16] We still don't have the ability to create or seed our data. So I can copy the code from my main method.

    [07:17 - 07:25] So from here, let me paste it below. Now let's get rid of these comments.

    [07:26 - 07:37] Since we don't need the host anymore, I can get rid of this line here. And instead of host, we can use app.services.createscope.

    [07:38 - 07:52] Finally, we can await app.run async. This file now has the exact same code we had inside two files, but now we don't need two files anymore to do the same job.

    [07:53 - 08:03] And now we can get rid of the code below. So we don't need the startup class anymore.

    [08:04 - 08:24] So let me get rid of this. We can now shut the server and restart it again to make sure everything is working as before.

    [08:25 - 08:37] As expected, we see no errors at all. And like I told you before, we can also remove using statements from our files and we can create a global file which will carry all these imports.

    [08:38 - 08:54] So inside our API project, we can create a new file and this will be called global usings.cs. You can call it anything, but this is just a convention to call it global us ings.

    [08:55 - 09:03] And inside this file, we can copy everything from imports from our program class. So let's cut it from here.

    [09:04 - 09:16] And let's paste it here. And now instead of using, I will select everything using here and place a keyword global in front of it.

    [09:17 - 09:27] And as soon as I have added global, we see no errors inside our program class. Now every file inside the API project will refer to the global usings file.

    [09:28 - 09:34] We can still use the local imports. This is just useful when we have the same using statements inside multiple files.

    [09:35 - 09:42] You can go to the individual files and shift all the using statements to the global usings file. But I think you got the point.

    [09:43 - 09:47] And now we have our project running in .net six made with minimal posting model . Amazing.