I have a .NET Standard 2.0 project that is being packed into a NuGet package upon build. (Using the .csproj approach to generate a package on build) This project contains a file ApiClientSettings.json
, which I want to copy to the output directory of the consuming project.
I have the following in my .csproj:
<ItemGroup>
<Content Include="ApiClientSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
According to MS documentation, the PackageCopyToOutput
attribute should do exactly what I want to do, but it does not seem to work when I install the package into a .NET Framework project. (Works perfectly for .NET Core project)
Am I missing anything else?
Here is my entire .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
<AllowedOutputExtensionsInPackageBuildOutputFolder>
$(AllowedOutputExtensionsInPackageBuildOutputFolder);.json
</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<IncludeContentInPack>true</IncludeContentInPack>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath></OutputPath>
</PropertyGroup>
<ItemGroup>
<Content Include="ApiClientSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.5" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbModelLibrary\DbModelLibrary.csproj">
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
<IncludeAssets>DbModelLibrary.dll</IncludeAssets>
</ProjectReference>
</ItemGroup>
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>
</Project>
Here is my generated .nuspec file aswell:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>Iso17025ApiClient</id>
<version>1.0.0</version>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Microsoft.AspNet.WebApi.Client" version="5.2.7" exclude="Build,Analyzers" />
<dependency id="Microsoft.Extensions.Configuration" version="3.1.5" exclude="Build,Analyzers" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="3.1.5" exclude="Build,Analyzers" />
<dependency id="Microsoft.Extensions.Http" version="3.1.5" exclude="Build,Analyzers" />
<dependency id="Newtonsoft.Json" version="12.0.3" exclude="Build,Analyzers" />
</group>
</dependencies>
<contentFiles>
<files include="any/netstandard2.0/appsettings.json" buildAction="Content" copyToOutput="true" />
</contentFiles>
</metadata>
</package>
I am adding the file structure of the consuming .NET Framework apps:
When the NuGet package reference is used, the file gets copied to the output directory, from the global package directory, but does not get added to the solution.
If the packages.config is used, the file gets copied to the solution but does not get marked as "Copy to Output" even though it is specified to do so in the NuGet package.
This probably has to do with the changes between pacakges.config and PackageReference NuGet package management? The difference between contentFiles and content probably.
What still confuses me, is that the .NET Core apps use PackageReference and the behaviour is different as with the .NET Framework apps. In the .NET Core apps the appsettings.json is added to the solution AND marked as "Copy to Output".
I would like the users to have roughly the same experience with the package no matter which target framework they are using. (I do not want them to manually add an appsettings.json JUST because they are using .NET Framework. Is this avoidable in some way?