About Hasmukh patel

My Photo
Harrow, London, United Kingdom
Dot-Net developer with expertise in Web, WPF, Win-form applications. Have worked on Asp.net,mvc , WPF and Win-forms projects in c#.net language having Sql-Server/Oracle as database with service oriented architecture using test driven development. Having complete knowledge of SDLC and have successfully worked and implemented it on projects.

Upgrading an existing .NET project files to the lean new CSPROJ format from .NET VS 2017

Upgrade csproj file with new 2017 style Project

You can upgrade two ways

  • Create new proejct using "Dotnet new classlib" and update manually.
  • Using 3rd party tool as below


Dotnet core Hans van Bakel's excellent CsProjToVS2017 global tool.
dotnet tool install Project2015To2017.Cli --global
Command "csproj-to-2017" and you can pass in a solution or an individual csproj.

More details at Scott Hanselman

Additions to the csproj format for .NET Core

Target Frameworks

Class library

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

Console app

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net46</TargetFramework>
  </PropertyGroup>
</Project>

Test project

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net46</TargetFramework>
  </PropertyGroup>
<ItemGroup>
    <!-- in projectA.csproj -->
<ProjectReference Include="..\ProjectB\ProjectB.csproj" />
</ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>
</Project>

Target Multiple Frameworks in .NET - Framework Specific References

Sometimes you may need to include specific references for a particular framework. For example, .NET Core 2.0 meta package already includes System.Net reference which is not included in .NET 4.0 and 4.5. So, we need to include it in .csproj file using conditional reference as shown below.
.csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp2.0;net45;net46</TargetFrameworks>
  </PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
    <Reference Include="System.Net" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
    <Reference Include="System.Net" />
  </ItemGroup>

</Project>