Vladimir Makaev
Vladimir Makaev

Reputation: 331

How to add <frameworkAssembly> into nuget package generated from csproj

I'm using VS2017 and the new csproj file format for creating nuget packages.

In my csproj file I have the following:

<ItemGroup>
   <Reference Include="System.Net" />
</ItemGroup>

Which works fine (TargetFrameworks == net45) when you build it. But when I pack it in nuget package I want the target package to have it as

<frameworkAssemblies>
  <frameworkAssembly assemblyName="System.Net" targetFramework="net45" />
</frameworkAssemblies>

How can I do that with this new tooling?

Upvotes: 1

Views: 1006

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

This is limitation of the current 1.0.* tooling. In the upcoming versions 1.1.* and 2.0.* versions of the ".NET SDK", this will be done automatically, with all <Reference> elements being added as framework assemblies to the resulting NuGet package (unless they are marked with Pack="false"). These changes will also be part of VS 2017 15.3 (not released yet at the time of writing). Note that i am talking about the tools (dotnet --version with SDK installed) version, not the .NET Core runtime versions.

There is a way to use the current preview packages of the pack targets, overriding the ones provided by the SDK - note that this is quite a hacky way and should be removed once you use the new 1.1 or 2.0 tooling.

<Project>
  <PropertyGroup>
    <NuGetBuildTasksPackTargets>junk-value-to-avoid-conflicts</NuGetBuildTasksPackTargets>
  </PropertyGroup>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

  <!-- All your project's other content here -->

  <ItemGroup>
    <PackageReference Include="NuGet.Build.Tasks.Pack" Version="4.3.0-preview1-4045" PrivateAssets="All" />
  </ItemGroup>
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

Also see the related GitHub issue on the NuGet repo where the information for this workaround originated from.

Upvotes: 1

Related Questions