0
votes

I am developing a library that targets net35 (.NET Framework 3.5), netstandard2.0 (.NET Standard 2.0) and netcoreapp3.1 (.NET Core 3.1) via TargetFrameworks.

For one of the functionalities, the library uses WPF internally so this functionality only works when targeting net35 (.NET Framework 3.5) and netcoreapp3.1 (.NET Core 3.1).

I use MSBuild pack target to create a NuGet package for my library but since WPF is used for netcoreapp3.1, I also create reference assembly for netcoreapp3.1 so that consumers on non-Windows platforms use that ref assembly when building their project as explained in this PackageReference support section.

My question is: how to instruct MSBuild pack target to include the reference assembly in 'ref/' folder of the created NuGet package?

1

1 Answers

1
votes

I also asked this question on NuGet's documentation GitHub and I got an answer that there is no native support for that, so I implemented a workaround and I am posting it here for others:

<!-- Generate also reference assembly. -->
<!-- See: -->
<!-- https://docs.microsoft.com/en-us/dotnet/standard/assembly/reference-assemblies#generating-reference-assemblies -->
<!-- https://github.com/dotnet/roslyn/blob/master/docs/features/refout.md -->
<PropertyGroup>
  <ProduceReferenceAssembly>true</ProduceReferenceAssembly>
</PropertyGroup>

<!-- This is a workaround until better support for \ref (Reference Assembly) is added in Pack target. -->
<!-- See: -->
<!-- https://docs.microsoft.com/en-us/nuget/create-packages/select-assemblies-referenced-by-projects#packagereference-support -->
<!-- Need a away to specify \ref (Reference Assembly) as target folder in Pack target (https://github.com/NuGet/Home/issues/4184) -->
<PropertyGroup>
  <!-- Supress warning NU5131 (https://docs.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5131) which is reported because .nuspec file is not updated -->
  <!-- to contain <references> element (https://docs.microsoft.com/en-us/nuget/reference/nuspec#explicit-assembly-references) for all files inside the 'ref\' directory.  -->
  <NoWarn>$(NoWarn),NU5131</NoWarn>
  <!-- See https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#targetsfortfmspecificcontentinpackage -->
  <TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);AddRefAssemblyToPackage</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>
<Target Name="AddRefAssemblyToPackage">
  <!-- Add reference assembly and XML documentation to 'ref/'. -->
  <ItemGroup Condition=" Exists('$(BaseOutputPath)$(Configuration)\$(TargetFramework)\ref\$(AssemblyName).dll') ">
    <TfmSpecificPackageFile Include="$(BaseOutputPath)$(Configuration)\$(TargetFramework)\ref\$(AssemblyName).dll" PackagePath="ref/$(TargetFramework)" />
    <TfmSpecificPackageFile Include="$(BaseOutputPath)$(Configuration)\$(TargetFramework)\$(AssemblyName).xml" PackagePath="ref/$(TargetFramework)" />
  </ItemGroup>
</Target>