"Could not load file or assembly" when trying to reference a library from a middle-man library.
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?
@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
@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?
@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.
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 [email protected] 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 moved to dotnet/cli #9659 via ZenHub