Dyanmically loading assemblies using AppDomain

F

Foehammer

Hello,
I'm trying to load an assembly dynamically using an app domain. This
is a proof-of-concept for a larger project, so please excuse the lame
class names.

TestLib is the dll where all the dynamic loading code will go. The
assemblies being dynamically loaded do not contain any code. They are
resource-only assemblies. I have succeeded in dynamically loading them
within the same AppDomain as the main application (a Winforms EXE that
references TestLib, but TestLib was the one doing the loading).
However, I'm stumped when trying to load them into a separate
AppDomain.

I have a class called Product that represents each dynamically loaded
plugin. I also have a generic class that I have created called
AssemblyWrapper. AssemblyWrapper has a property called
AssemblyFileName that is a string reference to an assembly's location
within the file system. AssemblyWrapper has a LoadAssembly method that
simply calls Assembly.Load with the path specified in Assembly file
name. Both classes are members of TestLib. Here is a segment of code
that attempts to load the assembly dynamically into the AppDomain:

public void LoadTestSet(string contentPath)
{
DirectoryInfo di = new DirectoryInfo(contentPath);
Assembly CurrentAssembly = Assembly.GetExecutingAssembly();
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.PrivateBinPath = di.Name;
setup.ApplicationName = this.productID;
this.ad = AppDomain.CreateDomain(this.productID, null, setup);
this.aw = (AssemblyWrapper)ad.CreateInstanceFromAndUnwrap("TestLib"
, "TestLib.AssemblyWrapper");

string filePath = contentPath + this.File;
aw.AssemblyPath = filePath;
aw.LoadAssembly();

//Do some other stuff - it fails before this point.
}

This code blows up on the ad.CreateInstanceFromAndUnwrap call. It says
it can't find TestLib. This is particularly interesting, as TestLib is
in the GAC. My post-build step calls gacutil /f /i $(targetPath) for
the TestLib dll.

Anybody have any ideas?

Thanks,
Will Gant
 
G

Guest

Arrgh. I'm such a lamer. I changed the code so as to call
Assembly.GetExecutingAssembly().Location to get the name of the TestLib
assembly in a way that allowed the resolver to work. Then, all I had to do
was make my AssemblyWrapper inherit from MarshalByRefObject so that it could
be serialized. It figures that I would find the answer within 5 minutes of
posting a question....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top