这两天把一个 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 文件,整个升级就完成了。

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


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2相关推荐

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

    .NET Core 3.0及ASP.NET Core 3.0 前瞻 ASP.NET Core 3.0 迁移避坑指南 将 ASP.NET Core 2.2 迁移至 ASP.NET Core 3.0 需要 ...

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

    阅读文本大概需要 3.3 分钟. 在上一篇文章<ASP.Net Core 运行错误 Http Error 502.5 解决办法> 的最后有提到说,最推荐的升级办法是从2.0升级到2.1X版 ...

  3. 将 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 Mi ...

  4. asp.net mvc相关开源项目推荐

    原文地址为: asp.net mvc相关开源项目推荐 asp.net mvc ctp版本发布不到一个月时间,在社区出现了丛多的优秀开源项目,社区的活跃性非常高哦,前一段时间园子里也引发了MVC和Web ...

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

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

  6. ASP.NET Core 介绍和项目解读

    1. 前言 2. ASP.NET Core 简介 2.1 什么是ASP.NET Core 2.2 ASP.NET Core的特点 2.3 ASP.NET Core 项目文件夹解读 2.3.1 项目文件 ...

  7. [译]使用LazZiya.ExpressLocalization开发多语言支持的ASP.NET Core 2.x项目

    介绍 开发多语言支持的ASP.NET Core 2.x Web应用程序需要大量的基础架构设置,并且耗费时间和精力.这篇文章,我们将使用LazZiya.ExpressLocalization nuget ...

  8. ASP.NET Core 实战:将 .NET Core 2.0 项目升级到 .NET Core 2.1

    一.前言  最近一两个星期,加班,然后回去后弄自己的博客,把自己的电脑从 Windows 10 改到 Ubuntu 18.10 又弄回 Windows 10,原本计划的学习 Vue 中生命周期的相关知 ...

  9. ASP.NET Core 开源论坛项目 NETCoreBBS

    ASP.NET Core 轻量化开源论坛项目,ASP.NET Core Light forum NETCoreBBS 采用 ASP.NET Core + EF Core Sqlite + Bootst ...

最新文章

  1. 1.A+B Problem
  2. java 字符表 chr3,VBS CHR码值对应列表
  3. 微软ODBC服务器驱动,Windows ODBC 驱动程序中的连接弹性
  4. android 480p分辨率,[RK3399][Android7.1] HDMI显示屏(副屏)调试记录小结
  5. 生产环境中,RabbitMQ 持续积压消息不进行ack ,发生什么了?
  6. sklearn 之 One-Class SVM的使用示例与解析
  7. CF1093D Beautiful Graph
  8. STEP 7-MicroWIN SMART 上传时搜索不到PLC
  9. ajax调用一般应用程序,【Web前端】---js调用本地应用程序
  10. 〔翻译〕摩托罗拉E680的linux系统解码
  11. pthread_create创建线程后是否需要释放资源
  12. matlab将三相电感进行d-q变换.,永磁同步电机交直轴电感计算
  13. C语言书籍阅读-读书笔记--《C专家编程》
  14. Windows挂起进程
  15. 【PCL】点云库PCL常见错误
  16. 给Web应用更换公众号步骤
  17. 《番茄工作法》让你的一天变成26小时
  18. 自己总结的html+css试题
  19. linux fq队列,QOS各种队列详解(FIFO,FQ,CBWFQ,PQ).doc
  20. React 教程及其API接口文档

热门文章

  1. Android帧缓冲区(Frame Buffer)硬件抽象层(HAL)模块Gralloc的实现原理分析(2)...
  2. swf 文件在线播放的,怎么能够下载呢?(除视频外其它都可)
  3. 为EasyUI 的Tab 标签添加右键菜单
  4. 在SQLSERVER企业管理器中如何创建触发器
  5. 6大奖项!首届 .NET 黑客松大赛圆满收官!
  6. .NET 6新特性试用 | 常量内插字符串
  7. SkyWalking配上告警更优秀
  8. 客户要求ASP.NET Core API返回特定格式,怎么办?
  9. WPF Grid动态显示或隐藏一列的一种方法
  10. 设计一个具有等待队列的连接池