Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assembly.Load does not find assembly in the application base directory #8045

Closed
slozier opened this issue May 10, 2017 · 7 comments
Closed

Comments

@slozier
Copy link
Contributor

slozier commented May 10, 2017

I tried the following:

Debug.Assert(File.Exists(Path.Combine(AppDomain.CurrentDomain.Base​Directory, "test2.dll")));
Assembly.Load("test2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");

which results in a FileNotFoundException. Works fine with .NET Framework.

All the documentation I'm finding relates to the .NET Framework and says that Assembly.Load will do probing in the application base directory if it fails to find the assembly elsewhere. If .NET Core is supposed to behave differently is there some documentation outlining the differences?

I guess as a workaround I could use AssemblyResolve and implement the probing myself?

@gkhanna79
Copy link
Member

I guess as a workaround I could use AssemblyResolve and implement the probing myself?

Yes, this is by design for .NET Core. https://github.com/dotnet/coreclr/blob/master/Documentation/design-docs/assemblyloadcontext.md talks about how assembly loading works in .NET Core.

@karelz
Copy link
Member

karelz commented May 18, 2017

@gkhanna79 if it is by design, can we close it?

@slozier
Copy link
Contributor Author

slozier commented May 18, 2017

Seems like a bit of an odd design choice. Makes it hard to use the Assembly.Load* methods on existing assemblies. For example I have A.dll which references B.dll. I place both files in the application folder and use Assembly.LoadFrom to load A. I then use a bit of reflection to invoke a method on A which uses something in B and I end up with a TargetInvocationException. It works fine with the .NET Framework and .NET Core 1.0 (doesn't work with 1.1 and 2.0).

I managed to work around it with AssemblyResolve but it seems like it's something that should just work out of the box.

@gkhanna79
Copy link
Member

The assembly binding behavior is, by design, distinctly different between Desktop and .NET Core. In 1.0, it happened to work due to a bug in the host that was fixed in 1.1.

Assembly.LoadFrom is a .NET Framework API that has been supported in 2.0 and for it, we emulate the desktop behavior.

@jkotas jkotas closed this as completed Oct 25, 2017
@jkotas
Copy link
Member

jkotas commented Oct 25, 2017

Also see dotnet/core-setup#2717 (comment)

@ankgupta067
Copy link

ankgupta067 commented Apr 5, 2018

@jkotas
how do we specify entry in runtimeconfig.json something like this, cant find an article hence posting here

"runtimeOptions": {
    "Microsoft.NETCore.DotNetHostPolicy.SetAppPaths" : true,
    "tfm": "netcoreapp2.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "2.0.0"
    },
    "configProperties": {
      "System.GC.Server": true
    }
  }

@b8adamson
Copy link

b8adamson commented Jan 16, 2019

FWIW This is the runtimeconfig.json that works for me:

{
"runtimeOptions": {
"tfm": "netcoreapp2.2",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "2.2.0"
},
"configProperties": {
"Microsoft.NETCore.DotNetHostPolicy.SetAppPaths": true
}
}

As best I can tell it gets used by the default dotnet.exe host here:
https://github.com/dotnet/core-setup/blob/v2.2.0/src/corehost/cli/hostpolicy.cpp#L170

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants