I want to define a NuGet package that contains two assemblies, one that is directly referenced by the consumer of my package and another one that is only needed at runtime and its APIs should not be visible to the consumer.
I've created a github repository to demonstrate the constellation in detail: https://github.com/lg2de/NuGetDependencyDemo
The assembly BaseLibrary is directly referenced by the assembly DependentLibrary.
BaseLibrary is an external assembly not available as NuGet package.
From DependentLibrary my NuGet package is created.
When creating project just referencing the NuGet package, the assembly BaseLibrary is missing in the build output. (In the mentioned repository you can find this constellation here.)
Of course, in this case the application raises an FileNotFoundException for BaseLibrary.
So I've added is to my NuGet package using targets file directive.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)BaseLibrary.dll" Visible="false">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
The assembly now available in the output folder, but I still get the same FileNotFoundException.
This seems to be new behavior in dotnetcore: Assemblies are not implicitly loaded.
The assembly is missing in ConsoleWithNugetReference.deps.json.
To solve this now have to load manually before it is first used.
var location = Path.GetDirectoryName(typeof(Program).Assembly.Location);
AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(location!, "BaseLibrary.dll"));
All this is very complicated and tedious!
So, how can I define a NuGet package that contains two assemblies, one that is directly referenced by the consumer of my package and another one that is only needed at runtime and its APIs should not be visible to the consumer?
What else have I tried:
- Adding asset attributes (like
IncludeAssets) on theReferenceelement.