这两天把一个 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 。

asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2 与正式版的更多相关文章

  1. ASP.NET Core 2.2 项目升级至 3.0 备忘录

    将 ASP.NET Core 2.2 迁移至 ASP.NET Core 3.0 需要注意的地方记录在这篇随笔中. TargetFramework 改为 netcoreapp3.0 <Target ...

  2. 将 ASP.NET Core 2.0 项目升级至 ASP.NET Core 2.1 RC 1

    今天微软发布了 .NET Core 2.1 RC 1 ,虽然只是 Release Candidate 版,但已经可以在生产环境中使用. NET Core 2.1 RC is supported by ...

  3. 将 ASP.NET Core 2.0 项目升级至 ASP.NET Core 2.1.3X

    在上一篇文章ASP.Net Core 运行错误 Http Error 502.5 解决办法的最后有提到说,最推荐的升级办法是从2.0升级到2.1X版本. 操作如下 项目的例子直接使用https://g ...

  4. 将 ASP.NET Core 2.0 项目升级至 ASP.NET Core 2.1

    主要升级步骤如下: 将 .csproj 项目文件中的 target framework 改为 netcoreapp2.1 <TargetFramework>netcoreapp2.1< ...

  5. ASP.NET CORE 2.0 文档中文正式版已经出来了

    https://docs.microsoft.com/zh-cn/aspnet/core/

  6. ASP.NET MVC5(一):ASP.NET MVC概览

    ASP.NET MVC概览 ASP.NET MVC是一种构建Web应用程序的框架,它将一般的MVC(Model-View-Controller)模式应用于ASP.NET框架. 1.ASP.NET MV ...

  7. SimpleMembership,成员资格提供程序、 通用的提供者和新的 ASP.NET 4.5 Web 窗体和 ASP.NET MVC 4 模板

    ASP.NET MVC 4 互联网模板中添加一些新的. 非常有用的功能,构建 SimpleMembership.这些更改将添加一些很有特色,像很多更简单. 可扩展会员 API 和 OAuth 的支持. ...

  8. .NET 6.0.6 和 .NET Core 3.1.26、Visual Studio 2022 17.2 和 17.3 Preview 2 和 .NET 7.0 Preview 5 同时发布

    Microsoft 昨天发布了适用于 .NET 6.0.6 和 .NET Core 3.1.26.NuGet.Visual Studio 2019 和 Visual Studio 2022 17.2 ...

  9. .NET跨平台之旅:将示例站点从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0

    终于将“.NET跨平台之旅”的示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 ,经历了不少周折,在这篇博文中记录一下. 从 AS ...

随机推荐

  1. 五种常见的ASP.NET安全缺陷

    保证应用程序的安全应当从编写第一行代码的时候开始做起,原因很简单,随着应用规模的发展,修补安全漏洞所需的代价也随之快速增长.根据IBM的系统科学协会(SystemsSciencesInstitute) ...

  2. Java list对象列表排序 实例

    package com.test; public class Bean { private String name; private int priority; public String getNa ...

  3. AJAX技术之DWR框架

    DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在 ...

  4. Atitit easyui翻页组件与vue的集成解决方案attilax总结

    Atitit easyui翻页组件与vue的集成解决方案attilax总结 ===============使用1 ===========\paggingUtil_easyui_vue.js2 C:\U ...

  5. 8个超实用的jQuery插件应用

    自jQuery诞生以来,jQuery社区都在不断地.自发地为jQuery创建许许多多功能不一的插件应用,很多jQuery插件非常实用,对我们的前端开发帮助相当大,不仅可以更完美的完成指定功能,而且节省 ...

  6. jquery checkbox checked 却不显示对勾

    $("input").attr("checked", true); 或 $("input").attr("checked" ...

  7. 再战android-语音识别1(科大讯飞)

    重新拾起刚入门的android,开发个小工具,一来不让自己离开发太远,二来看能否做出一个帮助自己管儿子学习的东西. 这次的主题是语音识别.稍微研究了下,开放的语音识别平台,本地识别的担心识别率问题,在 ...

  8. centos7 centos中apache运行php需要连接mysql一直连不上,telnet访问mysql出错Connection closed by foreign host

    执行命令: getsebool -a|grep httpd 发现 httpd_can_network_connect off 解决:  setsebool httpd_can_network_conn ...

  9. Git服务器环境搭建(打造属于自己的存储库)

    Git服务器搭建之CentOs7-阿里云搭建 首先使用连接工具连接到远程的阿里云服务器CentOs7上. 1.安装Git及创建用户 # 安装Git $ yum install git # 创建一个gi ...

  10. 生产环境使用nginx做负载均衡配置的五种策略

    nginx的upstream目前支持5种方式的分配1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight指定轮询几率,weight和访 ...