Here are some tips/examples on setting up Visual Studio 2017, that helped me streamline development cycle when working with PackageReference / SDK / .NET Standard project types and building NuGet packages.
The post covers:
- Building multiple targets (in this example: .NET 4.0, .NET 4.5 and .NET Standard)
- Setting different package references for different targets
- Setting documentation XML generation globally (for all build configuration, with dynamic file name)
- Automatically increase NuGet package version number
- Automatically copy build NuGet package to your local NuGet repository/library
The below are excerpts from Visual Studio .csproj XML files:
Setting up multi-target project
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
...
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net40'">
<DefineConstants>NET40</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45'">
<DefineConstants>NET45;NETFULL</DefineConstants>
</PropertyGroup>
The first node (TargetFrameworks) sets the targets. The first in the list will be selected by default for error interpretation/intellisense. Below, we are introducing compiler flags – so, we can use #if compiler instructions to add target specific source code.
Adding different references for different targets
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<PackageReference Include="System.Drawing.Primitives">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations">
<Version>4.4.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.ComponentModel.Annotations">
<Version>4.4.1</Version>
</PackageReference>
<PackageReference Include="System.Drawing.Primitives">
<Version>4.3.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
<Reference Include="System.ComponentModel.DataAnnotations">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll</HintPath>
</Reference>
</ItemGroup>
Here are defined references required for imbSCI.Core.
Setting documentation XML generation, for all build configurations, using default name for the XML documentation file
<PropertyGroup> <DocumentationFile>$(OutputPath)$(PackageId).xml</DocumentationFile> </PropertyGroup>
Making copy of output NuGet package to Local NuGet repository automatically, just after package is created:
<Target Name="CopyPackage" AfterTargets="Pack"> <Copy SourceFiles="$(OutputPath)$(PackageId).$(PackageVersion).nupkg" DestinationFolder="G:\imbVelesOpenSource\LocalNuGet\imbVelesSecondGeneration\" /> </Target>
Increase the Package version on each Release (re)build
Finally, found VS2017 extension for package version auto-increment, that meets my criteria: free, works with SDK/.NET Standard projects and is able to update NuGet package version number .
The last functionality, doesn’t setups well via provided GUI, but see my example from the project file XML and you’ll get the solution.
The extension url:
https://marketplace.visualstudio.com/items?itemName=PrecisionInfinity.AutomaticVersions
The <UpdatePackageVersion>True</UpdatePackageVersion> custom property is not set by GUI, therefore you’ll have to add it manually – as shown below:
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.0|AnyCPU'">
<ShouldCreateLogs>True</ShouldCreateLogs>
<AdvancedSettingsExpanded>True</AdvancedSettingsExpanded>
<UpdateAssemblyVersion>False</UpdateAssemblyVersion>
<UpdateAssemblyFileVersion>False</UpdateAssemblyFileVersion>
<UpdateAssemblyInfoVersion>False</UpdateAssemblyInfoVersion>
<UpdatePackageVersion>False</UpdatePackageVersion>
<AssemblyInfoVersionType>SettingsVersion</AssemblyInfoVersionType>
<InheritWinAppVersionFrom>None</InheritWinAppVersionFrom>
<WarningLevel>0</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
<ShouldCreateLogs>True</ShouldCreateLogs>
<AdvancedSettingsExpanded>True</AdvancedSettingsExpanded>
<UpdateAssemblyVersion>False</UpdateAssemblyVersion>
<UpdateAssemblyFileVersion>False</UpdateAssemblyFileVersion>
<UpdateAssemblyInfoVersion>False</UpdateAssemblyInfoVersion>
<UpdatePackageVersion>True</UpdatePackageVersion>
<AssemblyInfoVersionType>SettingsVersion</AssemblyInfoVersionType>
<InheritWinAppVersionFrom>None</InheritWinAppVersionFrom>
</PropertyGroup>
