otNet Core 2.0 推出來也有兩,三個月的時間了。現在團隊的項目是DotNet Core 1.0版本的,由於2.0並沒有向下兼容1.0,所以需要自己手動升級。
首先查看當前的dotnet core的版本,可以通過命令行查看,
$ 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的自動遷移之后,我們會發現
- 新增了一個*.csproj文件,這個文件是根據之前的project.json文件生產的,包好了我們項目運行需要的package和運行的環境變量
- 如果有sln文件,文件也會發生變化,變化的主要是GUID值,我們不需要去關心它。
- *.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>
把
<TargetFramework>netcoreapp1.0</TargetFramework>
換成
<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,那么代碼需要稍微修改一下,可以參考這篇文章。
