473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dyanmically loading assemblies using AppDomain

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
2 3092
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Krueger | last post by:
Hi I'm currently working on a forms designer for a free .NET IDE (SharpDevelop -> www.icsharpcode.net/OpenSource/SD). problem: I try to put 'custom' components (user controls from the current...
9
by: Ender | last post by:
I have an application that I would like third party developers to be able to create Plug-ins that will be dynamically loaded into our application to extend functionality. I have utilized the...
5
by: JonS. | last post by:
Hi, I posted this article ( http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.dotnet.languages.csharp&mid=0ee9781a-78f7-4398-a0ef-eeb195eccaea&sloc=en-us ) last week, and...
6
by: Pete Davis | last post by:
I'm confused about what precisely the limitations are on loading plugins in separate app domains. In all my previous apps that supported plugins, I've loaded them into the same domain as the app,...
2
by: Foehammer | last post by:
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...
1
by: Andrew Ducker | last post by:
I'm trying to load an assembly into a temporary AppDomain rather than my main AppDomain, so that it can be unloaded later on. However, it's also loading into my main AppDomain at the same time. ...
2
by: jnick | last post by:
I have the predicament of having to load several assemblies on the fly and when I do so, I get an exception stating that one of the referenced assemblies cannot be found. Is there any way to...
2
by: Jeff | last post by:
Hi I'm trying to achieve a scenario where I have c# files that are compiled dynamically, the assemblies are then loaded in a different AppDomain, I call a simple method from the object, and then...
1
by: =?Windows-1252?Q?Tor_B=E5dshaug?= | last post by:
BlankHi, I am having trouble loading assemblies from the database in my ASP.NET app. I have a default.aspx in my app that is served from a database via a custom virtual path provider. This works...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.