It's possible to load the same Assembly(DLL) twice to a single AppDomain
Issue Title
I ran into this issue in a more complicated scenario but it boils down to that you can load a single DLL Assembly into the currentDomain twice. The assembly appears twice in the AppDomain assemblies array and each one has it's own set of classes. As a result, you can have two singletons of the same class in a single AppDomain. This is not true for .net framework. I order to achieve this, all you need to do is load an assembly from file (which adds the assembly to the currentDomain) and then load the same assembly into the CurrentDomain.
Assembly assembly = Assembly.LoadFile(Path.GetFullPath(@"mydll.dll")); // Loads Assembly to AppDomain AppDomain.CurrentDomain.Load(assembly.GetName()); //Loads the same assembly a second time, but not in framework AppDomain.CurrentDomain.Load(assembly.GetName()); //Doesn't do anything in neither core nor framework
A small sample app is included in the ZIP file that does this. It contains a DLL (that does nothing) and the program itself.
General
Tested on framework 4.7.2 and core 3.1.302 and core 5.0.100-preview.6.20318.15. The issue happens in both core versions but not in framework. All tests were done on Windows 10 with visual studio 2019. SampleApp.zip
Tagging subscribers to this area: @vitek-karas, @agocke Notify danmosemsft if you want to be subscribed.
This behavior is by design on .NET Core - although compared to .NET Framework somewhat unfortunate.
On .NET Core there's only ever one AppDomain - so that can't be used to isolate assemblies (unlike .NET Framework). So instead there are load contexts (AssemblyLoadContext class - also called ALC). Each load context can only ever load one assembly of a given simple name. But it's possible to create any number of load contexts. There's only one special one the "Default" load context (AssemblyLoadContext.Default) which is the one with all of the framework assemblies in it, and typically the application itself (the assembly with Main).
Assembly loading methods have somewhat specific behavior in .NET Core with regard to load contexts:
Assembly.LoadorAppDomain.Load(they're identical on .NET Core) - "finds" the assembly in the load context of the caller - so a bit weird, but for normal code which doesn't use new load contexts it basically means it finds the assembly the default load context (it may end up also loading it from disk - but "find" is a better term as the caller doesn't specify the path).Assembly.LoadFrom- loads the specified file into the default load context (and always into the default load context, no matter where it's called from). If the default ALC already has an assembly with the same name this will typically fail. It has other specifics to it, but that's not relevant to this issue.Assembly.LoadFile- loads the specified file into its own and new load context - so this will never fail because of another assembly with the same name already loaded - it always creates a new load context and loads into it (well unless you call it on the same path twice, then it does that only on the first call, the second one will just return the exact same assembly as the first one - it doesn't load a second copy of the same file).
So in your example:
- The
LoadFilecall creates a new ALC and loads the file into it - returns assembly A-1 - The
Loadfinds that assembly in the default ALC (and since everything is lazy, this will probably end up loading it from disk) - typically this will only work on assemblies which are part of the app (so not a random file which happens to be next to the app). Since this is a different load context than the first, this returns assembly A-2 - The second
Loadfinds the assembly in the default ALC - this is just returning value from cache, so it directly returns A-2
If you don't want any of the load context magic and you also don't want to run into the possibility of loading multiple copies of the same assembly, then you should:
- Use
Loadfor assemblies which are part of the app (as the file path will be deduced from the app in .NET Core) - Use
AssemblyLoadContext.Default.LoadFromAssemblyPath- which is the most explicit way to load a file into the default load context (no ambiguity, no magic involved) - but this is .NET Core only - Use
Assembly.LoadFromwhich will always load to default ALC - and this should work more or less the same on .NET Framework
Thank you for the quick and detailed response. This level of information greatly helps understanding and resolving issues when moving from framework to core. I used this information and resolved the issue we had with double loading.
BTW, there is still an issue with visual studio where quickwatch shows one result and stepping through the code returns a different result. This happens when I used Assembly.Load() rather than LoadFrom() and the assembly is loaded twice. I can recreate this but it requires more complicated code with the use of 3rd party libraries. Not sure it's worth the trouble.
Re VS issue - I would have to see the specifics - but it's true that the VS debugger has only very basic support for load contexts - it should not fail on anything, but it's very possible that things don't work intuitively.