By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Dyanmically loading assemblies using AppDomain

P: n/a
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("T estLib"
, "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
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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....

"Foehammer" wrote:
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("T estLib"
, "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

Jul 21 '05 #2

P: n/a
This might answer any other questions you might have:

http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx

"foehammer" <fo*******@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
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....

"Foehammer" wrote:
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("T estLib"
, "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

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.