1

Does setting AssemblyName.CodeBase force the assembly to be loaded in the LoadFrom context?

I'm working on a kind of plugin system. I need to be able to use Type.GetType etc... At any place in my code I should not have to know if the Type come from a plugin dll or not.

My plugins are in a folder Plugins under the bin directory of my application (windows/web) and each plugin is in it's own folder.

I set my probing path to Plugins (or bin;bin\Plugins for web) even if it doesn't make any difference since they are in sub folders.

And I load my Plugin like this

pluginInfo.EntryAssemblyName = new AssemblyName(myAssemblyName);

pluginInfo.EntryAssemblyName.CodeBase = assemblyPath;

pluginInfo.EntryAssembly = Assembly.Load(pluginInfo.EntryAssemblyName);

Note: PluginInfo is just a class that keep the state of my plugin.

I wondering, because I set the CodeBase property of the assembly name Assembly.Load is able to find my assembly even if its not in the probing path, does it mean that the assembly get loaded in the default context or the load from context?

Is it normal that the AssemblyResolve event is raise after for the entry assembly again?

Yann Lebel
  • 675
  • 1
  • 7
  • 17

1 Answers1

0

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.

Panos Rontogiannis
  • 4,154
  • 1
  • 24
  • 29
  • My problem is if I do not set the CodeBase property it cannot find the Assembly because it's in a sub folder of the Plugins folder, and this folder is not in the probing path. Is it possible to set the probing path at runtime? – Yann Lebel Mar 29 '13 at 14:53
  • @Yann You can use the probing element in your application's configuration file. http://msdn.microsoft.com/en-us/library/823z9h8w.aspx – Panos Rontogiannis Mar 29 '13 at 15:28
  • The problem is, I don't know the folders before run time except if my plugins edit my config file which I don't really like. – Yann Lebel Mar 29 '13 at 17:11
  • @Yann Then you can consider the LoadFrom context. Here's a comparison of the three contexts: http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx – Panos Rontogiannis Mar 29 '13 at 18:22
  • Ok so I guess my current code is good. And will keep the Assembly.Load since at least it check the token and version even when it loads from the file. Thanks – Yann Lebel Mar 29 '13 at 18:42