August 12, 2021

New Project File Format (VS 2017/2019)

There is a new alternative project file format from Visual Stuidio 2017 onwards. Checkout this description here

Here is an example of a WPF project file
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

Note the "TargetFramework" of ".net5.0-windows" is required for WinForms, WPF applications. These are supported in .NET 5.0 but only as windows applications.

Here is a library project that will build to multiple .Net versions both core and frameworks

<Project Sdk="Microsoft.NET.Sdk">
  <!-- Main build info -->
  <PropertyGroup>
    <TargetFrameworks>net48;net5.0</TargetFrameworks>
    <PackageId>MyProject</PackageId>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <SignAssembly>false</SignAssembly>
  </PropertyGroup>
  
  <!-- Assembly Info -->
  <PropertyGroup>
    <AssemblyVersion>3.7.2.3</AssemblyVersion>
    <Description>An implementation of MyProject.</Description>
    <Authors>Joe Blogz</Authors>
    <Copyright>2019 Magicsoft Corp.</Copyright>
    <Company>Magicsoft Corp.</Company>
    <PackageTags>Subject1;Subject2</PackageTags>
    <PackageProjectUrl>https://myproject.com/url</PackageProjectUrl>
    <PackageLicenseExpression>MIT</PackageLicenseExpression>
    <PackageReleaseNotes>-</PackageReleaseNotes>
  </PropertyGroup>
  
  <!-- Repo Info -->
  <PropertyGroup>
    <RepositoryUrl>https://github.com/MyProject</RepositoryUrl>
    <RepositoryType>git</RepositoryType>
  </PropertyGroup>

  <!-- Remove AssemblyInfo.cs from build (in Properties dir.) -->
  <!-- it is now in the project file -->
  <ItemGroup>
    <Compile Remove="Properties\**" />
    <EmbeddedResource Remove="Properties\**" />
    <None Remove="Properties\**" />
  </ItemGroup>

  <!-- NUnit packages go here -->
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="System.ValueTuple" Version="4.5.0" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
    <Compile Remove="MyProject (net48)\**" />
  </ItemGroup>
</Project>

For multiple frame work targets <TargetFramework>..<TargetFramework> is replaced with <TargetFrameworks>...<TargetFrameworks> Note that .Net farmeworks use the style netnn where nn is the version but .Net version 5.0 onwards (which are .NET Core frameworks) use the form netn.n where n.n represesnt the .NET version. See https://docs.microsoft.com/en-us/dotnet/standard/frameworks for all possible entries

Note that the version info can be removed from the Assembly.cs and placed into the project file now. Referencing Nuget packages is also simplified.

Projects can be migrated with the help of the CsprojToVs2017 tool. Don't bother downloading the source and building it yourself, instead install it quickly using the dotnet install tool. This is described in the Quick Start Section. It will do most of the work of porting projects accross from the old format for you.

Some parts of a project file can be conditional, this feature is described here. For example in the project above I had to exclude some files that would only build in .NET Framework 4.8 or less. I placed these files in a subdirectory "CommonCore (net48)" and exclude them from the .NET 5.0 build.

No comments: