文章出處

這兩天把一個 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.6</TargetFramework>
  <AssemblyName>CNBlogs.Identity.ServiceAgent</AssemblyName>
  <PackageId>CNBlogs.Identity.ServiceAgent</PackageId>
  <NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
  <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
  <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
  <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>

改為

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

對于單元測試項目,TargetFramework 需要改為 netcoreapp2.0 ,比如

<PropertyGroup>
  <TargetFramework>netcoreapp1.1</TargetFramework>
  <AssemblyName>CNBlogs.Identity.UnitTests</AssemblyName>
  <PackageId>CNBlogs.Identity.UnitTests</PackageId>
  <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
  <RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
  <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
  <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
  <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>

改為

<PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

對于 web 項目,需要該動的地方很多。除了把 TargetFramework 改為 netcoreapp2.0 ,比如

<PropertyGroup>
  <TargetFramework>netcoreapp1.1</TargetFramework>
  <PreserveCompilationContext>true</PreserveCompilationContext>
  <AssemblyName>CNBlogs.Identity.Web</AssemblyName>
  <OutputType>Exe</OutputType>
  <PackageId>CNBlogs.Identity.Web</PackageId>
  <RuntimeIdentifiers>win10-x64;win8-x64;osx.10.12-x64;ubuntu.14.04-x64</RuntimeIdentifiers>
  <PackageTargetFallback>$(PackageTargetFallback);dnxcore50;portable-net45+win10</PackageTargetFallback>
  <RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
</PropertyGroup>

改為

<PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
</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<KeyManagementOptions>(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<Startup>()
            .Build();
    }
}

完成 web 項目升級后,升級所有 NuGet 包,刪除所有 AssemblyInfo.cs 文件,整個升級就完成了。

【更新】

2017年8月15日,微軟發布了 .NET Core 2.0 正式版,詳見 .NET Core 2.0 正式發布信息匯總 。

升級注意:

1)不再需要上面針對 PersistKeysToRedis() 的臨時解決方法

2)針對 Authentication 的修改

services.AddCookieAuthentication(options =>
{
    options.CookieHttpOnly = true;
    options.CookieName = "xxx";
    options.CookieDomain = "xxx";
    options.LoginPath = "/account/signin";
    options.LogoutPath = "/account/signout";
});

改為

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
    options.Cookie.HttpOnly = true;
    options.Cookie.Name = "xxx";
    options.Cookie.Domain = "xxx";
    options.LoginPath = "/account/signin";
    options.LogoutPath = "/account/signout";
});

3)視圖 .cshtml 中的變量名不能使用 page 。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    大師兄 發表在 痞客邦 留言(0) 人氣()