Search code examples
c#dependenciesnugetcsproj

Nuget dependencies are not copied to the target directory (net472 vs. net80)


I have a project that can be extended via plugins. The plugins are loaded via MEF, so there is no direct dependency from the EXE to the plugins.

The following image shows the Project dependencies. As you can see, there is no dependency from Executable to Plugin.

enter image description here

When I do a complete rebuild of the solution, all necessary dependencies for net472 are copied to the target directory, including Enums.NET.dll. For target platform net80 the dependencies are not copied.

As a consequence, the net472 build works as expected, whereas the net80 build does not find the Enums.NET.dll.

This is what the target directory for net80 looks like:

enter image description here

And this is how it looks at net472:

enter image description here

As you can see, the content differs significantly and therefore net80 does not work.

How can net80 be forced to copy all necessary DLL's into the target directory?

It is not caused by Enums.NET. The error also occurs with e.g. Microsoft.EntityFrameworkCore, but this is much larger and has further dependencies, which is why I did not use it for this example, because it only makes this sample unnecessarily complicated.

Context

CSPROJ of Executable:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net472;net80</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>latest</LangVersion>
    <OutputPath>$(SolutionDir)bin\$(Configuration)</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Contracts\Contracts.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.*" />
    <PackageReference Include="System.ComponentModel.Composition" Version="8.*" />
  </ItemGroup>

</Project>

CSPROJ of Plugin:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net472;net80</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>latest</LangVersion>
    <OutputPath>$(SolutionDir)bin\$(Configuration)</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Contracts\Contracts.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Enums.Net" Version="5.*" />
    <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.*" />
    <PackageReference Include="System.ComponentModel.Composition" Version="8.*" />
  </ItemGroup>

</Project>

Solution explorer view:

enter image description here


Solution

  • Try adding the following to your plugin .csproj under the property group.

    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    

    https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#copylocallockfileassemblies