Overview
Since .Net6.0 came out, I have always wanted to upgrade .Net6.0 to the previous project. Sometimes I think about it after all, there is a version 5.0 crossing in the middle. I don’t know if it’s a big deal when upgrading. I recently took some time to do some research on the upgrade plan and then upgraded the code to .Net6.0. In essence, I personally don’t like .Net6.0 removes the main method and startup. Microsoft’s way of doing this makes beginners learn is actually higher, but it can’t stop me from releasing the .Net6.0 project. The size of the release package is indeed small! Come on, let’s do it!
First, let’s look at the program code of asp.netcore3.1:
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => (args) .ConfigureWebHostDefaults(webBuilder => { <Startup>(); }); }
Secondly, let's look at the program code of core6.0
var builder = (args); // Add services to the container. (); (); (); var app = (); // Configure the HTTP request pipeline. if (()) { (); (); } (); (); (); ();
Compared with the 6.0 and 3.1 program code, there is one more WebApplication class, as a higher level of abstraction! Then there are no startup and main methods.
need
Because I still need to maintain the stratup for asp.netcore3.1, how can I continue to retain it in AspNet.Net6.0? I saw a code in the official document that can get the webhost
var builder = (args); (webBuilder => { <Startup>(); });
I thought this was so perfect! Then the operation reported an error saying it was not supported! ! You can get webhost, why don’t you support it? Friends who know can tell me that I will go and search for the source code later!
Current Solution
The first code case:
(args) .ConfigureWebHostDefaults(webBuilder => { <Startup>(); }).Build().Run();
Everyone must be familiar with this method, but I don’t need WebApplication directly, and it’s quite elegant!
The second code case:
var builder = (args); var startup = new Startup(); (); var app = (); (app, ); ();
It's a bit long-winded, but it works, and it also uses WebApplication!
After qualitative selection based on the above two solutions, the subsequent projects will be upgraded to third parties, which will be much faster! Here I will take my previous project as an example:
First change the file of the mvc project (csproj)
<Project Sdk=""> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <Nullable>disable</Nullable> <ImplicitUsings>disable</ImplicitUsings> <RootNamespace></RootNamespace> <GenerateDocumentationFile>False</GenerateDocumentationFile> <SignAssembly>False</SignAssembly> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <NoWarn>1701;1702;CS1591</NoWarn> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <NoWarn>1701;1702;CS1591</NoWarn> </PropertyGroup> <ItemGroup> <PackageReference Include="" Version="3.1.10" /> <PackageReference Include="" Version="3.1.17" /> <PackageReference Include="" Version="1.10.9" /> <PackageReference Include="" Version="3.1.5" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\\" /> </ItemGroup> </Project>
Note two points: In addition to changing the framework target to .Net6.0, I also set ImplicitUsings and Nullable to disable respectively.
Currently, VS2019 only supports the .Net6.0 preview version, and VS2022 supports the .Net6.0 project. In order to run this project on both VS2019 and 2022, ImplicitUsings is set to disable. As for Nullable to disable, it is to not want to see that annoying warning!
Next is the upgrade of the class library, which is even simpler.
<PackageReference Include="" Version="10.3.5" />
I have upgraded from version 8.0 to version 10.3.5, of course, the way it is used has also undergone some minor changes!
#3.1 Code public class LoginInputValidator : AbstractValidator<LoginInput> { public LoginInputValidator() { CascadeMode = ; RuleFor(x => ).NotEmpty().WithMessage("Please fill in the user name"); RuleFor(x => ).NotEmpty().WithMessage("Please fill in the user password"); RuleFor(x => ).NotEmpty().WithMessage("User number must be passed"); } }
#6.0 CodeCascadeMode = ;
#3.1mvc verification code (options => { var types = ("").GetTypes() .Where(e => ("Validator")); foreach (var item in types) { (item); } = false; }); #6.0 mvc verification code (options => { var types = ("").GetTypes() .Where(e => ("Validator")); foreach (var item in types) { (item); } = true; });
The rest are also some third-party library upgrades. Basically, nuget packages starting with Microsoft will be upgraded to 6.0.
Source code address:/shenniu_code_group/shen-nius.-modularity
Summarize
This is the article about the implementation of the method of upgrading the Asp.NetCore3.1 open source project to .Net6.0. For more related content on Asp.NetCore3.1 upgrade to .Net6.0, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!