catalogue
summary
since. Net6. After 0 came out, I always wanted to upgrade the previously developed project Net6. 0, sometimes think about it. After all, there is a version 5.0 in the middle. I don’t know how big the upgrade is. Recently, I took 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 Net6. 0 remove the main method and startup. Microsoft’s doing so makes the threshold for beginners to learn higher, but I like it Net6. The release package volume of 0 project is really small! Come on, let’s go!
First, let’s look at ASP netcore3. Program code of 1:
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<Startup>();
});
}
Secondly, let’s look at ASP net core6. Program code of 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();
Compared with the code of 3.1 program, 6.0 has one more webapplication class as a higher level of abstraction! Then the startup and main methods are missing.
demand
Because ASP netcore3. 1 project, I still need to maintain stratup, so in aspnet Net6. How to keep it in 0? I see such a code in the official document, which can get webhost
var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
I thought it was perfect! Then run the error report and say it is not supported!! You can get webhost. Why not? Friends who know can say it. Later, I’m going to turn over the source code and have a look!
Current solutions
The first code case:
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).Build().Run();
You must be familiar with this method, that is, I don’t need webapplication directly, and it’s 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 wordy, but it works. It also uses webapplication!
Based on the qualitative selection of the above two schemes, the latter project is to upgrade the third party, which is much faster! Here I take my previous project as an example:
First, change the file of the MVC project (csproj)
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<RootNamespace>ShenNius.Mvc.Admin</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="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.17" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ShenNius.Admin.API\ShenNius.Admin.API.csproj" />
</ItemGroup>
</Project>
Two points to note: in addition to changing the framework goal to Net6. In addition to 0, I set implicit uses and nullable to disable respectively.
Currently, vs2019 only supports Net6. 0 preview version, vs2022 support Net6. 0 project. In order to run this project on vs2019 and 2022, set implicit usage to disable, and nullable to disable to avoid the annoying warning!
The next step is to upgrade the class library, which is even simpler.
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.5" />
Put the previous fluent validation Aspnetcore has been upgraded from version 8.0 to version 10.3.5. Of course, some small changes have taken place in its use!
#3.1 code
public class LoginInputValidator : AbstractValidator<LoginInput>
{
public LoginInputValidator()
{
CascadeMode = CascadeMode.StopOnFirstFailure;
RuleFor(x => x.LoginName). NotEmpty(). Withmessage ("please fill in the user name");
RuleFor(x => x.Password). NotEmpty(). Withmessage ("please fill in the user password");
RuleFor(x => x.NumberGuid). NotEmpty(). Withmessage ("user number must be delivered");
}
}
#6.0 code
CascadeMode = CascadeMode.Stop;
#3.1mvc 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.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 about the upgrading of some third-party class libraries. Basically, it is right to upgrade nuget 3.1 packages starting with Microsoft to 6.0.
Source address:https://gitee.com/shenniu_code_group/shen-nius.-modularity
summary
This is about ASP NetCore3. 1. Upgrade the open source project to Net6. This is the end of the article on the implementation of the method of ASP. 0 NetCore3. 1 upgrade to Net6. Please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!