從.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,那麼代碼須要稍微修改一下,能夠參考這篇文章

相關文章
相關標籤/搜索