53

ASP.NET Core 3.0 迁移避坑指南 - 晓晨Master

 4 years ago
source link: https://www.cnblogs.com/stulzq/p/11497624.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

.NET Core 3.0将会在 .NET Conf 大会上正式发布,截止今日发布了9个预览版,改动也是不少,由于没有持续关注,今天将前面开源的动态WebApi项目迁移到.NET Core 3.0还花了不少时间踩坑,给大家分享一下我在迁移过程中遇到的坑。迁移的版本是当前Release最新版本 .NET Core 2.2 到 .NET Core 3.0 Preview 9。

二.ASP.NET Core 项目迁移

官方迁移文档:从 ASP.NET Core 2.2 迁移到3.0 ,这个官方文档比较详细,但是有一些东西里面并没有写。

1.更改框架版本

将 TargetFramework 版本改为 netcoreapp3.0

1568095449709

2.移除Nuget包

移除下图 Nuget 包

1568095550898

将其余 Nuget 包更新到支持 .NET Core 3.0 版本

3.Program更改

    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>();
                });
    }
1568095685774

4.Startup更改

ConfigureServices 方法:

  1. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 改为 services.AddControllers()(WebApi) / services.AddControllersWithViews();(MVC)

1568096481645

Configure 方法:

  1. 该方法里获取Host环境信息接口类型,IHostingEnvironment改为 IWebHostEnvironment

    1568096459106
  2. app.UseMVc 改为:

WebApi:

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseRouting();

            app.UseAuthorization();

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

关于Json组件

ASP.NET Core 3.0 默认移除了 Newtonsoft.Json ,使用了微软自己实现的 System.Text.Json,如果要改为 Newtonsoft.Json ,那么有以下两步:

1.安装Nuget包:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
services.AddControllers().AddNewtonsoftJson();

三.类库(Class Library Net Standard 2.0)项目迁移

因为 ASP.NET Core 3.0 对元包机制的改动,现在不能通过nuget安装 Microsoft.AspNetCore.All 或者 Microsoft.AspNetCore.App 3.0版本,以及他们包含的大多数Nuget包也不能通过nuget安装了(没有3.0对应的版本)。如果说还引用2.2版本的nuget包,那么运行起来可能会出错。元包被包含在了 .NET Core SDK中,这意味着如果我们的类库项目依赖了 AspNetCore 相关组件,那么将没法继续将项目目标框架设置为 .NET Standard 了,只能设置为.NET Core 3.0,因为 **ASP.NET Core 3.0 only run on .NET Core **。

元包机制改动原因:https://github.com/aspnet/AspNetCore/issues/3608

1568097757661

1.更改框架版本

1568097890464

2.更新Nuget包

移除 Microsoft.AspNetCore.* 不具有 .NET Core 3.0 的版本,例如:

1568097911477

添加 FrameworkReference(不是 PackageReference) 引用:

1568097942488

题外话:ASP.NET Core 直到2.2 是可以同时运行在 .NET Framework 和 .NET Core 中,但是从 ASP.NET Core 3.0 开始,将会只支持 .NET Core。

相关资料:A first look at changes coming in ASP.NET Core 3.0

上面说的改动,微软官方都有解释原因,其实是为了变得更好而改动,弥补以前的缺点,只不过对于用了这么久的Core来说有点折腾,但是还是能接受,为了更好的 .NET Core。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK