asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2 与正式版
2017-08-05 16:07
1011 查看
这两天把一个 asp.net core 1.1 的项目迁移到了 asp.net core 2.0 preview 2 ,在这篇随笔中记录一下。
如果项目在有 global.json 文件,需要删除或修改为 .net 2.0 preview 2 的 sdk 版本号。
对于类库项目的 .csproj,需要把 TagetFramework 改为 netstandard2.0 ,比如
改为
对于单元测试项目,TargetFramework 需要改为 netcoreapp2.0 ,比如
改为
对于 web 项目,需要该动的地方很多。除了把 TargetFramework 改为 netcoreapp2.0 ,比如
改为
还需要:
1)移除所有对 Microsoft.AspNetCore 的引用
添加 Microsoft.AspNetCore 的引用
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 时出现下面的错误
这是由于 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() 方法不起作用,需要改用临时解决方法:
详见 PersistKeysToRedis not working in .NET Core 2.0 Preview 2 with Redis 0.1.2
5)Startup 的修改
去除构造函数中下面的代码
去除 Configure 方法中下面的代码
IConfigurationRoot 改为 IConfiguration
6)Program 的修改
完成 web 项目升级后,升级所有 NuGet 包,删除所有 AssemblyInfo.cs 文件,整个升级就完成了。
【更新】
2017年8月15日,微软发布了 .NET Core 2.0 正式版,详见 .NET Core 2.0 正式发布信息汇总 。
升级注意:
1)不再需要上面针对 PersistKeysToRedis() 的临时解决方法
2)针对 Authentication 的修改
由
改为
3)视图 .cshtml 中的变量名不能使用 page 。
如果项目在有 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
- 升级 asp.net core 1.1 到 2.0 preview
- ASP.NET Core 1.1 Preview 1 简介(包含.NETCore 1.1升级公告)
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
- Asp.net1.1升级到2.0遇到的一些问题总结
- ASP.NET MVC2.0的项目如何升级到3.0??
- 本周任务asp.net 1.1老系统移植升级到asp.net 2.0,又是一个浩大的工程啊?
- 将ASP.NET Core2.0项目部署在IIS上运行
- Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例
- 从ASP.NE T 1.1升级到ASP.NET 2.0需要考虑的Cookie问题
- 在阿里云Windows Server 上部署ASP .NET CORE2.0项目
- Asp.NET Core2.0 项目实战入门视频课程_完整版
- Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例
- Asp.Net Core 项目从 1.0.1 升级到 1.1.0 的小补丁
- ASP.NET 1.1 升级至 ASP.NET 2.0 十个问题总结
- 用VSCode开发一个基于asp.net core 2.0/sql server linux(docker)/ng5/bs4的项目(1)
- Asp.NET Core2.0 项目实战入门视频课程_完整版
- Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级
- Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例
- ASP.NET Core 2.0 : 三. 项目结构