1

In .NET core, the generated .deps.json file controls assembly loading - if your dependencies aren't in the .deps.json for your top level application, they will not get loaded unless you start handling AssemblyResolve events and all that stuff.

The situation I have is as follows

.NET Core 6

Class Library Assembly - lets call it 'ClassLib'

Application (exe) - lets call it 'App' - that depends on 'ClassLib' as a project reference

If I use a Nuget package (PackageReference) inside ClassLib then the Nuget package shows up in the generated App.deps.json and everything works. (Newtonsoft.json used as an example of this below)

However, I have several cases where there are legacy assemblies that I wish to reference that are not in Nuget packages. Those can be added as references using the UI (Add COM Reference then 'Browse' to the assembly) or via a <Reference ...> node in the csproj.

When you build 'App', the App.deps.json does not include any sign of the dependencies on the legacy assemblies via ClassLib, just the nuget packages. This means that at runtime, the legacy assembly is not going to get loaded, leading to all sorts of interesting failures...

Details of the situation

ClassLib.csproj contents

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="Legacy">
      <HintPath>..\path\to\Legacy.dll</HintPath>
      <SpecificVersion>True</SpecificVersion>
    </Reference>
  </ItemGroup>

</Project>

App.csproj contains

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

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

</Project>

Generated App.deps.json shows the dependency of ClassLib on NewtonSoft.Json (imported as Nuget) but not on Legacy.dll

      "ClassLib/1.0.0": {
        "dependencies": {
          "Newtonsoft.Json": "13.0.2"
        },
        "runtime": {
          "ClassLib.dll": {}
        }
      }

I have tried various combinations of options in the node such as CopyLocal/Private etc with no change to the outcome in terms of the generated App.deps.json

I can make things work if I pack Legacy.dll into a nuget package, but to be honest I have a number of legacy dlls to deal with and making each into a nuget package (they come from various sources and may be updated separately) seems rather a 'sledgehammer to crack a nut' solution.

so...

Is there a way that I can persuade the build system to treat the old-fashioned assembly reference in the same way as the package reference and propagate the dependencies up to higher level projects? Failing that, is there a way that you can customize the build process to inject dependencies into the .deps.json file at build time? (hey, a different sort of dependency injection!) Or am I stuck making nuget packages or hacking around in AssemblyResolve events?

RobinG
  • 369
  • 2
  • 6
  • Is Legacy copied over to the output directory? If it is, then it should also get loaded. "deps.json" as far as I know is only meant to figure out what shared framework the referenced projects are using. When you reference a dll directly then there is no information about the shared framework. As long as the dll is in the same output folder it should get loaded. See also https://natemcmaster.com/blog/2017/12/21/netcore-primitives/#depsjson So in my opinion deps.json does not define what gets loaded. Only what additional stuff needs to be loaded from e.g. the shared framework. – Wolfspirit Nov 30 '22 at 00:40
  • Yes, the legacy.dll gets copied to the output directory by the build system, so the file is in place. I just discovered that if I delete the .deps.json file then it works. That tallies with info here - https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/default-probing - that indicates that the when the deps.json file is missing the application directory is used to resolve references. However with the deps.json file present, only assemblies listed in it are loaded (need to find reference for that - seen it before but cannot find link right now). – RobinG Nov 30 '22 at 13:28
  • Ok, it looks like it is the same as or similar to this issue - https://stackoverflow.com/questions/60609087/visual-studio-2019-not-adding-referenced-projects-to-deps-json . Applying the suggested solution there of cleaning nuget package cache, cleaning build, deleting obj folders and rebuilding seems to have worked for my sample project. I don't understand why or whether it will stay working yet. – RobinG Nov 30 '22 at 13:38

0 Answers0