In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to upgrade the Asp.NetCore3.1 open source project to .net 6.0, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Overview
Since the advent of .Net6.0, I have been thinking about upgrading the previously developed project. Net6.0. Sometimes I think that after all, there is a 5.0 version in the middle. I don't know whether the upgrade is big or not. Recently, I took the time to do some research on the upgrade scheme, and then upgraded the code to .Net6.0. In essence, I personally don't like it very much. with the removal of main method and startup in Net6.0, Microsoft makes it more difficult for beginners to learn, but it can't stop me from liking it. Net6.0 project release package is really small and so on! Come on, let's do it!
First, let's take a look at asp.netcore3.1 's program code:
Public class Program {public static void Main (string [] args) {CreateHostBuilder (args). Build (). Run ();} public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .ConfigureWebHostDefaults (webBuilder = > {webBuilder.UseStartup ();});}
Second, let's take a look at the program code of asp.net core6.0.
Var builder = WebApplication.CreateBuilder (args); / / Add services to the container.builder.Services.AddControllers (); builder.Services.AddEndpointsApiExplorer (); builder.Services.AddSwaggerGen (); var app = builder.Build (); / / Configure the HTTP request pipeline.if (app.Environment.IsDevelopment ()) {app.UseSwagger (); app.UseSwaggerUI ();} app.UseHttpsRedirection (); app.UseAuthorization (); app.MapControllers (); app.Run ()
6. 0 compared with 3.1program code, there is one more WebApplication class, as a higher level of abstraction! Then the startup and main methods are missing.
Demand
Because I still need to keep stratup in the asp.netcore3.1 project, how can I keep it in AspNet.Net6.0? I saw a line of code in the official document that can get the webhost.
Var builder = WebApplication.CreateBuilder (args); builder.Host.ConfigureWebHostDefaults (webBuilder = > {webBuilder.UseStartup ();})
I thought it was perfect! Then run the error report and say it is not supported! Can get webhost, why not support, friends who know can say, after I am going to look at the source code to have a look!
Current solution
The first code case:
Host.CreateDefaultBuilder (args) .ConfigureWebHostDefaults (webBuilder = > {webBuilder.UseStartup ();}) .Build () .HostDefaults ()
This way you must be very familiar with, that is, I directly do not need WebApplication, but also more elegant!
The second code case:
Var builder = WebApplication.CreateBuilder (args); var startup = new Startup (builder.Configuration); startup.ConfigureServices (builder.Services); var app = builder.Build (); startup.Configure (app, app.Environment); app.Run ()
It's a little verbose, but it works, and it also uses WebApplication!
Based on the qualitative selection of the above two schemes, the following project is to upgrade the third party, the speed is much faster! Here I will take my previous project as an example:
First change the file of the mvc project (csproj)
Net6.0 disable disable ShenNius.Mvc.Admin False False 1701 / 1702 / 1701 / 1701 / 1702 / 1702 / 1702 / 1702 / 1702 / 1702 / 1702 / 1702 / 1702 / 1702 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1701 / 1702 / 1701 / 1701 / 1701 / 1702 / 1701 / 1701 / 1701 / 1701 / 1702 / 1701 / 1701 / 1702 / 1701 / 1701 / 1702 / 1701 / 1702 / 1701 / 1701 / 1702 / 1701 / 1701 / 1702 / 1701 / 1701 / 1702 / 1701 / 1701 / 1702 / 1701 / 1701 / 17
Note two points: in addition to changing the framework target to .net 6.0, I set ImplicitUsings and Nullable to disable, respectively.
At present, VS2019 only supports .Net6.0 preview version, and VS2022 supports .Net6.0 project. In order to make this project run on both VS2019 and 2022, set ImplicitUsings to disable and Nullable to disable in order not to see the annoying warning!
The next step is to upgrade the class library, which is even easier.
Upgrade the previous FluentValidation.AspNetCore from 8.0 to 10.3.5, of course, there have been some minor changes in the way it is used!
# 3.1 Code public class LoginInputValidator: AbstractValidator {public LoginInputValidator () {CascadeMode = CascadeMode.StopOnFirstFailure; RuleFor (x = > x.LoginName) .NotEmpty () .WithMessage ("Please fill in user name"); RuleFor (x = > x.Password) .NotEmpty () .WithMessage ("Please fill in user password") RuleFor (x = > x.NumberGuid). NotEmpty (). WithMessage ("user number must be passed");}} # 6.0Code CascadeMode = CascadeMode.Stop;#3.1mvc authentication code mvcBuilder.AddFluentValidation (options = > {var types = Assembly.Load ("ShenNius.Share.Models"). GetTypes () .Where (e = > e.Name.EndsWith ("Validator")) Foreach (var item in types) {options.RegisterValidatorsFromAssemblyContaining (item);} options.RunDefaultMvcValidationAfterFluentValidationExecutes = false;}) # 6.0 mvc verification code mvcBuilder.AddFluentValidation (options = > {var types = Assembly.Load ("ShenNius.Share.Models"). GetTypes () .Where (e = > e.Name.EndsWith ("Validator")); foreach (var item in types) {options.RegisterValidatorsFromAssemblyContaining (item) } options.DisableDataAnnotationsValidation = true;})
The rest is also a matter of upgrading third-party libraries, basically upgrading from 3.1s of nuget starting with microsoft to 6.0s.
The above is all the contents of the article "how to upgrade an Asp.NetCore3.1 open source project to .net 6.0". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 302
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.