By using the AssemblyName(string) .ctor and setting the AssemblyName.CodeBase
property, you cannot be sure that Assembly.Load(AssemblyName) will load it in the default context. It will first try to load it in the default context but if this fails, it will use the codebase to load it in the LoadFrom context. Have a look at Suzanne Cook's blog entry on the subject for more information.
To be sure that the assembly will be loaded in the default context, do not set the AssemblyName.CodeBase
property and be prepared for a System.IO.FileLoadException. If the object is constructed successfully (no exception is thrown), then the assembly was loaded in the default context.
The advantage of the LoadFrom context is that the dependencies of the loaded assembly can be resolved if they exist on the same path. On the other hand, assemblies loaded on the default context can be resolved only if the exist on the GAC, the AppDomain.BaseDirectory
or the AppDomain.RelativeSearchPath
. The AppDomain.AssemblyResolve
event is probably raised because a dependency of the loaded assembly cannot be found in these loactions.