0

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 Answer 1

2

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://learn.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://learn.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://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5131) which is reported because .nuspec file is not updated -->
  <!-- to contain <references> element (https://learn.microsoft.com/en-us/nuget/reference/nuspec#explicit-assembly-references) for all files inside the 'ref\' directory.  -->
  <NoWarn>$(NoWarn),NU5131</NoWarn>
  <!-- See https://learn.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>
Sign up to request clarification or add additional context in comments.

1 Comment

According to this document: learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/6.0/…, ref assemblies are not created in the output path anymore, but instead in the "IntermediateOutputPath". The Property "TargetRefPath" could be used to get the path. Maybe the answer should be updated accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.