Search code examples
c#visual-studiopackage.net-assembly

Prevent copying of package reference using Visual Studio Publish


A C# assembly i am working on is referencing the package Newtonsoft.Json, which is automatically copied when using the Publish function in Visual Studio.

I am using the Publish function to copy the assembly into a Unity project folder, which is also referencing Newtonsoft Json using its internal package management. The result is that Unity has two conflicting DLLs which it keeps bugging me about. The resolution is to remove one of them, but Visual Studio keeps copying the DLL back in every time I publish.

Solutions that should work, but don't:

  • I found a lot of references to some "Copy to Output Directory" setting which exists for most files, but not for package references.

Any other file: Settings for any other file

Package Reference Properties: Settings for Package Reference

  • I also found references to the CopyToPublishDirectory setting in the .csproj, which i tried, but it either doesn't work or I can't get the syntax right.
  1. In the Package Reference:
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" CopyToPublishDirectory="Never" />
  </ItemGroup>

  1. In a Content Update block:
  <ItemGroup>
    <Content Update="Newtonsoft.Json.dll">
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </Content> 
  </ItemGroup>

  • I tried some other Setting that might work in the .csproj:
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <ExcludeFilesFromDeployment>Newtonsoft.Json.dll</ExcludeFilesFromDeployment>
  </PropertyGroup>

Solution

  • Clean project. Add ExcludeAssets="runtime"

    <PackageReference Include="Newtonsoft.Json" Version="13.0.3"  ExcludeAssets="runtime"/>