1
votes

I need to register some type(s) e.g MyHandler: IRequest in autofac. These types resides in an assembly 'A' that is not referenced so far and hence not loaded in appdomain. I tried to load assembly 'A' using Assembly.LoadFile(path), it gets loaded in app domain, but later this same dll is loaded in appdomain again by .net runtime and there are two instances of 'A' in app domain, which is a problem in my scenario. using Autofac registration soure won't help either. I just want to know to prevent loading of 'A' by runtime, as I have already loaded it.

1
LoadFile() is wrong in 99.9% of all cases, use LoadFrom() instead. You only ever use LoadFile() if you want to load an assembly more than once, that's very rare.Hans Passant

1 Answers

4
votes

Through LoadFile method you can load assemblies multiple times from different PATHS. Instead of LoadFile() try LoadFrom() or simply Load() the last methods ensures that your assembly is loaded only once.

https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assembly.loadfrom?view=netframework-4.8