Upgrade to Visual Studio 2017 version 15.3

Recently, Visual Studio 2017 version 15.3 was released. And I would like to share my experience with the upgrade. This version is the first one that supports .NET Core 2.0 and .NET Standard 2.0. As we would like to convert our projects to use these frameworks, first step is to upgrade Visual Studio.

Background

Few months ago we started to use .NET Core SDK for development in our new projects. It has several advantages:

  • Good support in Visual Studio 2017.
  • Very simple .csproj file. This helps a lot, when reviewing Git pull requests.
  • It can target .NET Framework, .NET Core, .NET Standard, or all at once.
  • Integration of NuGet directly in .csproj file.
  • Automatic use of Roslyn compiler. No need to reference Microsoft.Net.Compilers NuGet.

Example of .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net47</TargetFramework>
	  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>..\..\..\build\</OutputPath>
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
    <TreatSpecificWarningsAsErrors />
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <OutputPath>..\..\..\build\</OutputPath>
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
    <TreatSpecificWarningsAsErrors />
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Microsoft.AnalyzerPowerPack" Version="1.1.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="1.1.0" />
    <PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
    <PackageReference Include="System.Runtime.Analyzers" Version="1.1.0" />
    <PackageReference Include="System.Runtime.InteropServices.Analyzers" Version="1.1.0" />
  </ItemGroup>

  <Import Project="..\..\global\ERNI.Shared\ERNI.Shared.projitems" Label="Shared" />
</Project>

You may notice, NuGet references are specified directly in this file using PackageReference element. There are no Compile elements, because all *.cs files in the folders and subfolders are included in compilation by default. Last element is Import that references a shared project. Shared projects in Visual Studio started as solution to share source code between projects targeting different mobile platforms. However, this feature is much more powerfull and can be used in plenty of use cases. In our case, we share some build properties (assembly and file version, static analysis rule set, etc.) between all projects in solution.

FxCop analyzers for Roslyn

Maybe you are familiar with FxCop tool. This tool runs static analysis on your code and finds possible issues like incorrect implementation of Dispose, or potentially unsafe string comparison. When Microsoft introduced Roslyn compiler to .NET community, it was natural to implement Roslyn analyzers with FxCop rules. The result is Microsoft.CodeAnalysis.FxCopAnalyzers NuGet package. You may find it referenced in the example above.

After upgrading Visual Studio 2017 to version 15.3, it was not possible to restore this FxCopAnalyzers package. It failed with the message that it was not possible to restore some dependencies.

Solution

Failed dependencies were Desktop.Analyzers 1.0.0 and System.Security.Cryptography.Hashing.Algorithms.Analyzers 1.0.0. However, version 1.0.0 of these packages is not available in NuGet store. Available stable version is 1.1.0. So I tried to add explicit dependency to this version. It failed using NuGet manager in Visual Studio. Then I edited .csproj file directly and added PackageReference elements for both packages.

Now the NuGet section in .csproj file looks like this:

  <ItemGroup>
    <PackageReference Include="Desktop.Analyzers" Version="1.1.0" />
    <PackageReference Include="Microsoft.AnalyzerPowerPack" Version="1.1.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="1.1.0" />
    <PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
    <PackageReference Include="System.Runtime.Analyzers" Version="1.1.0" />
    <PackageReference Include="System.Runtime.InteropServices.Analyzers" Version="1.1.0" />
    <PackageReference Include="System.Security.Cryptography.Hashing.Algorithms.Analyzers" Version="1.1.0" />
  </ItemGroup>

And the solution builds up successfully.

What’s next?

Next step is to convert from ASP.NET Core 1.1 to 2.0. Stay tuned.

And hopefully Microsoft will release a new version of Microsoft.CodeAnalysis.FxCopAnalyzers package soon. There have been some beta releases, but the stable release is 2 years old.