从.Net Core 1.0升级到2.0的一次经历
2017-10-24

DotNet Core 2.0 推出来也有两,三个月的时间了。现在团队的项目是DotNet Core 1.0版本的,由于2.0并没有向下兼容1.0,所以需要自己手动升级。

 

首先查看当前的dotnet core的版本,可以通过命令行查看,

1
$ dotnet -v

也可以通过控制面板的“添加删除程序”查看,这里包含Runtime和SDK两个安装包。在升级到dotnet core 2.0之前,可以选择要不要删除之前安装的老版本的Runtime和SDK,如果不删除的话,系统默认会使用最新的版本。我们这边选择删除以前老的Runtime和SDK

删除完老版本的Dotnet Core后,我们需要去官网下载最新的2.0版本, 并进行安装。

 

假设之前的1.0项目名字叫做“DemoVersion1”,dotnet core 2.0 提供了很好的CLI命令行,可以帮助我们做迁移,这里要使用的命令行叫

$ dotnet migrate
 
$ dotnet migrate project.json
Summary
 
Total Projects: 1
 
Succeeded Projects: 1
 
Failed Projects: 0
 
The project migration has finished. Please visit https://aka.ms/coremigration to report any issues you've encountered or ask for help.
 
Project `Acn.School.csproj` added to the solution.
 
Project reference `Acn.School.xproj` removed.
 
Files backed up to C:\git\DemoVersion1\backup\

 

在dotnet core的自动迁移之后,我们会发现

  1. 新增了一个*.csproj文件,这个文件是根据之前的project.json文件生产的,包好了我们项目运行需要的package和运行的环境变量
  2. 如果有sln文件,文件也会发生变化,变化的主要是GUID值,我们不需要去关心它。
  3. *.xproj和package.json文件被删除了。

 

我们主要关系csproj文件,我们会发现TargetFramework还是"netcoreapp1.0", dotnet core包引用的还是"1.0.3"版本,这里需要做一下版本的修改。

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <AssemblyName>Acn.School</AssemblyName>
    <OutputType>Exe</OutputType>
    <PackageId>Acn.School</PackageId>
    <RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
    <PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
    <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="MailKit" Version="1.16.1" />
    <PackageReference Include="HtmlAgilityPack" Version="1.5.0-beta9" />
    <PackageReference Include="Microsoft.Extensions.Caching.Redis.Core" Version="1.0.3" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
    <PackageReference Include="System.ServiceModel.Duplex" Version="4.0.1" />
    <PackageReference Include="System.ServiceModel.Http" Version="4.1.0" />
    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.1.0" />
    <PackageReference Include="System.ServiceModel.Security" Version="4.0.1" />
    <PackageReference Include="System.Xml.XmlSerializer" Version="4.0.11" />
</ItemGroup>
</Project>

  

1
<TargetFramework>netcoreapp1.0</TargetFramework>

换成

1
<TargetFramework>netcoreapp2.0</TargetFramework>

 

将下面两行删除

<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
 
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>

 

将所有的AspNetCore和EF的版本,全部换成2.0.0

<ItemGroup> <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
</ItemGroup>

  

这个时候如果运行项目,会发现很多的依赖包找不到,我们需要先运行一下

1
$ dotnet restore

 

如果之前有使用StyleCop.Analyzers做代码的分析检查,那么需要

将project.json文件中的

    "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "define": [],
    "additionalArguments": [ "/ruleset:RuleSet.ruleset" ]
},

修改为,RuleSet.ruleset是自己定制的ruleset文件

1
<CodeAnalysisRuleSet>RuleSet.ruleset</CodeAnalysisRuleSet>

 

如果项目中有引用WCF service的话,那么需要在VS 2017里面安装一个拓展:

Microsoft WCF Web Service Reference Provider

 

官网地址是:https://marketplace.visualstudio.com/items?itemName=WCFCORETEAM.VisualStudioWCFConnectedService

已经开始支持dotnet core 2.0了,但是目前还是有些版本兼容的问题,感兴趣的同学可以关注下这个issue:https://github.com/dotnet/wcf/issues/2340

 

 一个work around的方法是,在Visual Stduio Installer中安装“.NET Core 1.0-1.1 development tools”