question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Could not load file or assembly" when trying to reference a library from a middle-man library.

See original GitHub issue

@VictorioBerra commented on Sun May 06 2018

Could not load file or assembly when trying to reference a library from a middle-man library.

dotnet --version = 2.1.105

General

If I have a netcoreapp2 project (console) that references another netcoreapp2 project (library), which references a netstandard2 .DLL somewhere locally on my machine, I get an error “Could not load file or assembly” for that DLL.

But it works just fine if I move that dll reference to the console app and invoke it there instead of the intermediate library.

Project 1 (Console)

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

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

</Project>
using ClassLibrary1;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            new TestClass().CreateInstanceOfStandardLib();
        }
    }
}

Project 2 (Class Library)

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="ClassLibraryToBeBuiltAndCopied">
      <HintPath>..\ClassLibraryToBeBuiltAndCopied\bin\Debug\netstandard2.0\ClassLibraryToBeBuiltAndCopied.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>
using ClassLibraryToBeBuiltAndCopied;

namespace ClassLibrary1
{
    public class TestClass
    {
        public void CreateInstanceOfStandardLib()
        {
            new Class1();
        }
    }
}

Notice the reference in the class library up there, that is a simple netstandard2.0 project with a single empty class in it. I went ahead and ran dotnet build .\ClassLibraryToBeBuiltAn dCopied.csproj and then I pointed the Class Library at the resulting dll. When I start the console app, I get this:

System.IO.FileNotFoundException: 'Could not load file or assembly 'ClassLibraryToBeBuiltAndCopied, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'

This is completely resolved if I reference the standard library in my console app and use it directly instead of trying to use it through an intermediary library. What am I doing wrong? How can I fix this?


@uffebjorklund commented on Sun May 06 2018

@VictorioBerra Can confirm that I get the same result.

To get it working I had to load the external reference. The file is gettings copied into the correct place, but for some reason it cant be resolved

public class TestClass
{
    public TestClass()
    {
            if (isDebug)
            {
                var ext = Path.Combine(Directory.GetCurrentDirectory(), "bin/Debug/netcoreapp2.0/", "Ext.dll");
                System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(ext);
            }
            else
            {
                var ext = Path.Combine(Directory.GetCurrentDirectory(), "Ext.dll");
                System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(ext);
            }
    }
    public void CreateInstanceOfStandardLib()
    {
        new Ext.ClassA();
    }
}

This is probably not what you want, but if you go this way you need this package in your csproj:

 <ItemGroup>
    <PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
  </ItemGroup>

Sample Repo: https://github.com/uffebjorklund/ExternalReferenceNetCore


@VictorioBerra commented on Sun May 06 2018

@uffebjorklund thank you for looking into this and replying. Yes that solution is something I would really like to avoid. I wonder if this is a bug that was introduced with the recent SDK/Runtime update?


@uffebjorklund commented on Sun May 06 2018

@VictorioBerra I am not sure how this is supposed to work, I have never used Ref with HinPath. Added a global.json to my sample repo and tested with the oldest sdk on my machine, same issue. However, that was 2.1.4 (not to old). Saw some AspNet.Core config with that setup and I doubt that they load assemblies manually. Maybe I am missing something, or maybe it really is a bug.


@VictorioBerra commented on Sun May 06 2018

Well I was able to create the bug using visual studio without ever touching the .csproj files. I right click the references and added the assembly using browse. so I think something is not working right.

On Sun, May 6, 2018, 3:13 PM Uffe Björklund notifications@github.com wrote:

@VictorioBerra https://github.com/VictorioBerra I am not sure how this is supposed to work, I have never used Ref with HinPath. Added a global.json to my sample repo and tested with the oldest sdk on my machine, same issue. However, that was 2.1.4 (not to old). Saw some AspNet.Core config with that setup and I doubt that they load assemblies manually. Maybe I am missing something, or maybe it really is a bug.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet/core/issues/1501#issuecomment-386909789, or mute the thread https://github.com/notifications/unsubscribe-auth/ACzG620CeKXisfeqOPSzJ2Uh8DdEdfl3ks5tv1l8gaJpZM4T0Hb0 .

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:23 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
VictorioBerracommented, Jun 27, 2019

I think you might be right. I think this is still an issue.

1reaction
dsplaistedcommented, Dec 10, 2020

Everything works when the dll is referenced with HintPath, but when the dll is copied manually or from code in custom MSBuild target it still doesn’t work. While HintPath solution is good enough for me, I’d like to understand better why manually copied assemblies cannot be copied at runtime. Where can I learn more about it?

By default the DLL needs to be listed in the deps.json file. Here are some links with more information:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not load file or assembly or one of its dependencies
the first line where the Unity assembly is loaded, double click it and see the calling assembly, and here you go.
Read more >
LINQPad 6 error with referenced library.
I have a DLL I am referencing in a query and I am getting this error: FileNotFoundException: Could not load file or assembly...
Read more >
Two sub library projects in one solution use same dll but ...
I have a solution with multiple projects. There is a need that I have to refer to a different version of the same...
Read more >
Minuteman Library Network
The Minuteman Library Network is comprised of 41 public and academic libraries in the MetroWest region of Massachusetts.
Read more >
dynamically adding assembly reference to executing ...
Hi, I have been searching around to try adding reference assembly to another assembly during runtime, programatically. Is this possible
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found