.net core 2.2 升级到 3.0 正式版

官方操作手册 https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio

结合到自己的项目上,需要的步骤记录如下:

更改目标框架版本, VS操作项目属性即可

完成后,更新 csproj 文件

<TargetFramework>netcoreapp3.0</TargetFramework>

删除 Microsoft.AspNetCore.* 的引用

web项目删除所有项目的 Microsoft.AspNetCore.App 引用,需要用文本编辑器修改项目的 csproj 文件
类库项目删除包引用Microsoft.AspNetCore.App 引用, 并加入框架引用

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

添加nuget包引用

YuanFxCore.Infrastructure加入

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />

YuanFxCore.Web和YuanFxCore.Dashboard加入

<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />

YuanFxCore.Service加入

<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />

YuanFxCore.Repository加入

<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />

在YuanFxCore.Web和YuanFxCore.Dashboard的csproj中PropertyGroup里加入

<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
删除 <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>

Program.cs

由于托管方式的变化,整个 Program.cs (包括AutoFac的注入方式也改变了)

        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            // ASP.NET Core 3.0+:
            // The UseServiceProviderFactory call attaches the
            // Autofac provider to the generic hosting mechanism.
            var host = Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory()) // 启动Autofac
                .ConfigureWebHostDefaults(webHostBuilder =>
                {
                    webHostBuilder
                      .UseContentRoot(Directory.GetCurrentDirectory())
                      .UseIISIntegration()
                      .UseStartup<Startup>();
                });

            return host;
        }

Startup.cs

新增引用

using Microsoft.Extensions.Hosting; 
using Microsoft.AspNetCore.SpaServices.Extensions; 

ConfigureServices 方法中

将AddJsonOptions改为AddNewtonsoftJson

将 CompatibilityVersion.Version_2_2 改为 CompatibilityVersion.Version_3_0

Configure 方法中

修改 Configure 的构造函数

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions senparcWeixinSetting)

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions senparcWeixinSetting)

在 if (env.IsDevelopment()) 之前加入

app.UseRouting();
app.UseCors();
在 
// 注册用户认证中间件
app.UseAuthentication();
后面加入
app.UseAuthorization();

将 UseMvc 改为 UseEndpoints, UseEndpoints 函数中的内容改为

endpoints.MapControllers();
endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapFallbackToController("Index", "Home");

删除 app.MapWhen

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据