从.Net Core 1.0升级到2.0的一次经历

otNet Core 2.0 推出来也有两,三个月的时间了。如今团队的项目是DotNet Core 1.0版本的,因为2.0并无向下兼容1.0,因此须要本身手动升级。html

 

首先查看当前的dotnet core的版本,能够经过命令行查看,git

$ dotnet -v

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

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

 

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

$ 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的自动迁移以后,咱们会发现app

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

 

咱们主要关系csproj文件,咱们会发现TargetFramework仍是"netcoreapp1.0", dotnet core包引用的仍是"1.0.3"版本,这里须要作一下版本的修改。ide

<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>

  

ui

<TargetFramework>netcoreapp1.0</TargetFramework>

换成spa

<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>

  

这个时候若是运行项目,会发现不少的依赖包找不到,咱们须要先运行一下

$ dotnet restore

 

若是以前有使用StyleCop.Analyzers作代码的分析检查,那么须要

将project.json文件中的

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

修改成,RuleSet.ruleset是本身定制的ruleset文件

<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”

 最后,若是项目中有用到Microsoft.AspNetCore.Authorization中的JWT,那么代码须要稍微修改一下,能够参考这篇文章

相关文章
相关标签/搜索