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

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

时间: 2024-08-09 02:09:11

asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2的相关文章

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

在上一篇文章ASP.Net Core 运行错误 Http Error 502.5 解决办法的最后有提到说,最推荐的升级办法是从2.0升级到2.1X版本. 操作如下 项目的例子直接使用https://github.com/52ABP/52ABP.School?作为对象,毕竟他正好是.NET CORE 2.0的版本. 首先要下载SDK包. 地址:https://www.microsoft.com/net/download 下载最新的.NET Core 2.1的版本. 打开LTM.School项目后,

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

ASP.NET MVC概览 ASP.NET MVC是一种构建Web应用程序的框架,它将一般的MVC(Model-View-Controller)模式应用于ASP.NET框架. ASP.NET MVC模式简介 MVC将Web应用程序划分为三个主要的部分,以下是MSDN给出的定义: 模型(Model):模型对象是实现应用程序数据域逻辑的应用程序部件. 通常,模型对象会检索模型状态并将其存储在数据库中. 例如,Product 对象可能会从数据库中检索信息,操作该信息,然后将更新的信息写回到 SQL S

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

终于将".NET跨平台之旅"的示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 ,经历了不少周折,在这篇博文中记录一下. 从 ASP.NET 5 到 ASP.NET Core 最大的变化,除了改名之外,就是用 dotnet cli(命令名是dotnet)取代了dnx.所以运行 ASP.NET Core 程序,首先要安装 dotnet cli,我们是在 Ubuntu 服务器上用 apt-get install dotn

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 项目文件夹总览 2.3.2 project.json和global.json 2.3.1 Properties——launchSettings.json 2.3.4 Startup.cs (1) 构造函数 (2) ConfigureServices (3) Configure 2.3.5 bundlecon

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

原文:ASP.NET Core 实战:将 .NET Core 2.0 项目升级到 .NET Core 2.1  一.前言  最近一两个星期,加班,然后回去后弄自己的博客,把自己的电脑从 Windows 10 改到 Ubuntu 18.10 又弄回 Windows 10,原本计划的学习 Vue 中生命周期的相关知识目前也没有任何的进展,嗯,罪过罪过.看了眼时间,11月也快要结束了,准备补上一篇如何将我们的 .NET Core 2.0 版本的程序升级到 .NET Core 2.1 版本,好歹也算多学

学习ASP.NET Core Razor 编程系列五——Asp.Net Core Razor新建模板页面

学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET Core Razor 编程系列三——创建数据表及创建项目基本页面 学习ASP.NET Core Razor 编程系列四——Asp.Net Core Razor列表模板页面 上一篇文章中我们学习了列表页面的结构,@page与@model两个关键Razor指令,以及页面布局应该修改哪里.这一篇文章我们来

ASP.NET Core 设置和初始化数据库 - ASP.NET Core 基础教程 - 简单教程,简单编程

原文:ASP.NET Core 设置和初始化数据库 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 设置和初始化数据库 上一章节中我们已经设置和配置好了 EF 框架服务,本章节我们就来学习如何使用 EF 框架设置和初始化数据库 初始化数据库 初始化数据库的方法之一是使用 EF 框架来创建数据库,仅仅需要两步就能完成 第一步,给我们的 HelloWorld 项目添加迁移 ( migration ) 代码 迁移代码是 C# 代码,用来在数据库系统中创建数据库

ASP.NET Core 配置 EF 框架服务 - ASP.NET Core 基础教程 - 简单教程,简单编程

原文:ASP.NET Core 配置 EF 框架服务 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 配置 EF 框架服务 上一章节中我们了解了 Entity Framework 的基本工作原理和 DbContext ,我们也创建了一个自己的 HelloWorldDBContext. 本章节我们就来讲讲如何设置我们的 EF 框架来链接到 SQLite 数据库 配置 EF 框架服务 要让我们的 EF 框架的 DBContext 能够运行起来,我们需要更改一

ASP.NET Core 配置 EF SQLite 支持 - ASP.NET Core 基础教程 - 简单教程,简单编程

原文:ASP.NET Core 配置 EF SQLite 支持 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 配置 EF SQLite 支持 上一章节我有提到 macOS 版的 Visual Studio Community 没有携带 LocalDB,也就是说 LocalDB 暂时不支持 macOS 系统 虽然我可以在 Windows 上继续完成接下来的教程,但我觉得还是感觉不妥,如果其它使用苹果笔记本的人要去哪里找 Windows 的电脑 我临时改变