asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2

2021-06-11 23:05

阅读:357

标签:onchange   settings   sync   figure   issue   epo   doc   test   group   

这两天把一个 asp.net core 1.1 的项目迁移到了 asp.net core 2.0 preview 2 ,在这篇随笔中记录一下。 

如果项目在有 global.json 文件,需要删除或修改为 .net 2.0 preview 2 的 sdk 版本号。

对于类库项目的 .csproj,需要把 TagetFramework 改为 netstandard2.0 ,比如

PropertyGroup>
  TargetFramework>netstandard1.6TargetFramework>
  AssemblyName>CNBlogs.Identity.ServiceAgentAssemblyName>
  PackageId>CNBlogs.Identity.ServiceAgentPackageId>
  NetStandardImplicitPackageVersion>1.6.1NetStandardImplicitPackageVersion>
  GenerateAssemblyConfigurationAttribute>falseGenerateAssemblyConfigurationAttribute>
  GenerateAssemblyCompanyAttribute>falseGenerateAssemblyCompanyAttribute>
  GenerateAssemblyProductAttribute>falseGenerateAssemblyProductAttribute>
PropertyGroup>

改为

PropertyGroup>
  TargetFramework>netstandard2.0TargetFramework>
PropertyGroup>

对于单元测试项目,TargetFramework 需要改为 netcoreapp2.0 ,比如

PropertyGroup>
  TargetFramework>netcoreapp1.1TargetFramework>
  AssemblyName>CNBlogs.Identity.UnitTestsAssemblyName>
  PackageId>CNBlogs.Identity.UnitTestsPackageId>
  GenerateRuntimeConfigurationFiles>trueGenerateRuntimeConfigurationFiles>
  RuntimeFrameworkVersion>1.1.1RuntimeFrameworkVersion>
  GenerateAssemblyConfigurationAttribute>falseGenerateAssemblyConfigurationAttribute>
  GenerateAssemblyCompanyAttribute>falseGenerateAssemblyCompanyAttribute>
  GenerateAssemblyProductAttribute>falseGenerateAssemblyProductAttribute>
PropertyGroup>

改为

PropertyGroup>
  TargetFramework>netcoreapp2.0TargetFramework>
PropertyGroup>

对于 web 项目,需要该动的地方很多。除了把 TargetFramework 改为 netcoreapp2.0 ,比如

PropertyGroup>
  TargetFramework>netcoreapp1.1TargetFramework>
  PreserveCompilationContext>truePreserveCompilationContext>
  AssemblyName>CNBlogs.Identity.WebAssemblyName>
  OutputType>ExeOutputType>
  PackageId>CNBlogs.Identity.WebPackageId>
  RuntimeIdentifiers>win10-x64;win8-x64;osx.10.12-x64;ubuntu.14.04-x64RuntimeIdentifiers>
  PackageTargetFallback>$(PackageTargetFallback);dnxcore50;portable-net45+win10PackageTargetFallback>
  RuntimeFrameworkVersion>1.1.1RuntimeFrameworkVersion>
PropertyGroup>

改为

PropertyGroup>
  TargetFramework>netcoreapp2.0TargetFramework>
PropertyGroup>

还需要:

1)移除所有对 Microsoft.AspNetCore 的引用

PackageReference Include="Microsoft.AspNetCore.Authentication" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.DataProtection.Redis" Version="0.1.2" />
PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.Hosting" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.1" />
PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.2" />
PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />

添加 Microsoft.AspNetCore 的引用

PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview2-final" />

2)修改 Authentication 相关的代码

  • CookieAuthenticationOptions 的命名空间改为 Microsoft.AspNetCore.Authentication.Cookies
  • HttpContext.Authentication.SignInAsync() 改为 HttpContext.SignInAsync() ,需要安装 NuGet 包 Microsoft.AspNetCore.Authentication.Abstractions ,引用命名空间 Microsoft.AspNetCore.Authentication 。 
  • cookieAuthOptions.Value.AuthenticationScheme 改为 CookieAuthenticationDefaults.AuthenticationScheme

3) 针对 BundlerMinifier 的修改

在 asp.net core 2.0 preview 2 的 docker  容器中 build 项目,在执行 dotnet bundle 时出现下面的错误

It was not possible to find any compatible framework version
The specified framework ‘Microsoft.NETCore.App‘, version ‘1.1.0‘ was not found.
- Check application dependencies and target a framework version installed at:
    /
- Alternatively, install the framework version ‘1.1.0‘.

这是由于 nuget 包 BundlerMinifier.Core 不支持 .net core 2.0 。github 上签出 BundlerMinifier 的源码(已增加了对.net core 2.0的支持),自己打包。 

升级时不仅要升级 PackageReference 中的 BundlerMinifier.Core ,还要升级 DotNetCliToolReference 中的 BundlerMinifier.Core 。

4)针对 DataProtection 的修改

Microsoft.AspNetCore.DataProtection.Redis 的 PersistKeysToRedis() 方法不起作用,需要改用临时解决方法:

services.Configure(o =>
{
    o.XmlRepository = new RedisXmlRepository(() => redisConn.GetDatabase(), "DataProtection-Keys-Cnblogs");
});

详见 PersistKeysToRedis not working in .NET Core 2.0 Preview 2 with Redis 0.1.2

5)Startup 的修改

  • 去除构造函数中下面的代码
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    
    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
  • 去除 Configure 方法中下面的代码
    loggerFactory.AddSerilog();
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  • IConfigurationRoot 改为 IConfiguration
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    
    public IConfiguration Configuration { get; set; }

6)Program 的修改

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((context, logging) =>
            {
                logging.AddSerilog();
            })
            .UseStartup()
            .Build();
    }
}

完成 web 项目升级后,升级所有 NuGet 包,删除所有 AssemblyInfo.cs 文件,整个升级就完成了。

asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2

标签:onchange   settings   sync   figure   issue   epo   doc   test   group   

原文地址:http://www.cnblogs.com/dudu/p/7266134.html


评论


亲,登录后才可以留言!