Migrate csproj file from VS 2015 to VS 2017 and .NET SDK

With the release of Visual Studio 2017 and the introduction of .NET Core, Microsoft has implemented a new version of csproj files, simplier, lighter and easier to mantain and modify.

The new version has nothing in common with the older and if we want to migrate the older Visual Studio 2015 projects to the newer format, we had to become familiar with the new format and with the options that we have.

Recently I migrate some projects so I want to share my experience with this activity.

.NET SDK

In order to build the new .NET Core applications and the new Visual Studio 2017 project files, Microsoft introduced the .NET SDK, that give us some stuff to manage our projects.

With the .NET SDK we can restore NuGet packages, build projects, run tests, installing applications; is automatically embedded in Visual Studio 2017, so we don’t care to installing it but we’ll consider to do that if we have a continuous integration process, where the build process will be done in an enviroment without Visual Studio 2017 installed.

With this concept in the mind we can proceed to examine the format of the new csproj file.

The new format

If we create a new .NET Core project and edit the csproj file, it will look like this:


<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>

The first tag specity the project SDK we talked about before and in the TargetFramework we can setup the framework/s of the application.

As you can see we don’t have anymore the single dll references and this is a huge improvement about the readability and the maintenance of the csproj file.

If we add a nuget package, we’ll see the reference in an item group section:

<ItemGroup>
 <PackageReference Include="newtonsoft.json" Version="7.0.1" />
</ItemGroup>

Assembly Version

In the older Visual Studio 2015 projects we  specified the Assembly Info version in the related AssemblyInfo.cs file.

With the new format, we can delete this file and use a new section in the project properties:

We can find and edit these informations in the csproj file as well:

 
<Project Sdk="Microsoft.NET.Sdk">
 <PropertyGroup>
 <TargetFramework>netcoreapp2.0</TargetFramework>
 <Company>TestCompany</Company>
 <Authors>TestCompany</Authors>
 <Version>1.0.0</Version>
 <Product>TestProject</Product>
 </PropertyGroup>
</Project>

Migration

Now we are ready to migrate our Visual Studio 2015 project to a Visual Studio 2017.

I have done that manually, so what was previously this:


<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A57A2D17-93F0-4849-8259-877829EBD649}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>xxx</RootNamespace>
<AssemblyName>xxx</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\Net45\</OutputPath>
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>6</LangVersion>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\Net45\</OutputPath>
<DefineConstants>TRACE;NET45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
...List of project files...
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
</Project>

Became:


<Project Sdk="Microsoft.NET.Sdk">
 <PropertyGroup>
 <TargetFrameworks>net35;net45</TargetFrameworks>
 <PackageId>xxx</PackageId>
 <Authors>xxx</Authors>
 <Description></Description>
 <Copyright>Copyright © 2018</Copyright>
 <AssemblyVersion>1.0.0.0</AssemblyVersion>
 <FileVersion>1.0.0.0</FileVersion>
 <Company>xxx</Company>
 </PropertyGroup>
 <ItemGroup>
 <PackageReference Include="newtonsoft.json" Version="7.0.1" />
 <PackageReference Include="SharpZipLib" Version="0.86.0" />
 </ItemGroup>
 <ItemGroup>
 <Reference Include="System.Web" />
 </ItemGroup>
 <ItemGroup>
 <Folder Include="Properties\" />
 </ItemGroup>
</Project>

I plan to move all the company projects (around 50) from the older format to the newer.

In addition to simplify the structure of the project files, this change will erase all the conflicts that now we have when two developers makes some changes to the dependencies of the project; as we don’t have anymore references in the project, we’ll no longer have conflicts about it.

 

 

One thought on “Migrate csproj file from VS 2015 to VS 2017 and .NET SDK

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s