從 ASP.NET Core 5.0 遷移到.NET 6


.net6發布后,迫不及待的將.net5的一個項目遷移到了.net6,這里記錄遷移過程中遇到的問題,詳細的遷移可以參考微軟這個地址 參考

先決條件

首先要安裝最新版的vs2022才能開發.net6項目。我這里沒有直接去.net5項目上做更改,而是用vs2022新建一個.net6的asp.netcore web空項目,然后再把.net5的代碼copy過來

變化點

.net6最大的變化在於Startup.cs和 Program.cs文件

最小宿主模型:

大大減少了創建應用程序所需的文件和代碼行數。 只有一個文件需要四行代碼。
Startup.cs將和 Program.cs 合並到單個 Program.cs 文件中。
使用 頂級語句 來最大程度地減少應用程序所需的代碼。
使用全局 using 指令消除或最大程度地減少所需的 using 語句行數。

以上是微軟官方的話語,其實就是微軟化繁為簡,把之前startup里的代碼又放回了program里面,同時為了省事把main函數以及花括號通通刪掉,namespace也做了優化,最終我們看到的program變成了這樣

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
   app.UseExceptionHandler("/Error");
   // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
   app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

在我們的.net5項目里,我們使用了NewtonsoftJson組件以及IHttpContextAccessor來獲取httpcontext,startup類的代碼如下

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson(options =>
            { 
                // 設置時間格式
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; 
                //忽略null值
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            }); 
            services.AddMvc();
            services.AddHealthChecks();
            //注入HttpContextAccessor
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHttpContextAccessor accessor)
        {
            //設置HttpContextAccessor實例
            HttpContextHelper._accessor = accessor;
            ... 
    }

.net6的方案

根據微軟官方說法

  • ConfigureServices 已替換為 WebApplication.Services 。
  • builder.Build() 返回配置 WebApplication 為該變量的 app 。 Configure 使用對相同服務的配置調用將替換為 app 。
    於是我們的Program.cs代碼改為
builder.Services.AddControllers().AddNewtonsoftJson(options =>
{ 
    // 設置時間格式
    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; 
    //忽略null值
    options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
}); 
//注入HttpContextAccessor
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
...
var app = builder.Build();
//初始化HttpContextAccessor實例
var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
HttpContextHelper._accessor = httpContextAccessor;

當然我們可以通過擴展方法來優化下代碼

 
    public static void AddHttpContextAccessor(this IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    } 
    public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app)
    {
        var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
        HttpContextHelper._accessor = httpContextAccessor;
        return app;
    }
 

通過以上改造,.net6項目正常運行了,后面就是把.net5項目的業務代碼copy到新項目即可
最終的代碼如下

 
using Microsoft.AspNetCore.ResponseCompression;
using Swashbuckle.AspNetCore.Swagger;

var builder = WebApplication.CreateBuilder(args); 
// Add services to the container.
builder.Services.AddControllers().AddNewtonsoftJson(options =>
{ 
    // 設置時間格式
    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; 
    //忽略null值
   options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
}); 
builder.Services.AddResponseCompression(options =>
{
    options.Providers.Add<GzipCompressionProvider>();
}); 
builder.Services.AddMvc();
builder.Services.AddHealthChecks();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new  Microsoft.OpenApi.Models.OpenApiInfo { Title = "My-API", Version = "v1" });
    var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); 
    var xmlPath = Path.Combine(basePath, "SwaggerDemo.xml");
    c.IncludeXmlComments(xmlPath);
});

//注入HttpContextAccessor
builder.Services.AddHttpContextAccessor(); 
var app = builder.Build(); 
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My-API");
    });
}

//設置HttpContextAccessor實例
app.UseStaticHttpContext();
...
app.Run();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM