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

@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 .

About this issue

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

Most upvoted comments

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

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:

Issue is very much open. I have

  1. Azure function with .NETCore.App 2.0 referencing
  2. Class Library with NET Standard Library 2.0.3

Azure function cannot load the Class Library at runtime and runs into: Exception thrown: ‘System.IO.FileNotFoundException’ in System.Private.CoreLib.dll Could not load file or assembly <Project at no 2 above>

Will be really helpful if someone has any pointers.

@VictorioBerra Can you try this with the latest .NET Core SDK (2.1.302). There have been a few fixes to raw file references since 2.1.105.

https://www.microsoft.com/net/download/windows