473,789 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
AssemblyFileNam e 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(str ing contentPath)
{
DirectoryInfo di = new DirectoryInfo(c ontentPath);
Assembly CurrentAssembly = Assembly.GetExe cutingAssembly( );
AppDomainSetup setup = new AppDomainSetup( );
setup.Applicati onBase = AppDomain.Curre ntDomain.BaseDi rectory;
setup.PrivateBi nPath = di.Name;
setup.Applicati onName = this.productID;
this.ad = AppDomain.Creat eDomain(this.pr oductID, null, setup);
this.aw = (AssemblyWrappe r)ad.CreateInst anceFromAndUnwr ap("TestLib"
, "TestLib.Assemb lyWrapper");

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.CreateInstan ceFromAndUnwrap 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 3141
Arrgh. I'm such a lamer. I changed the code so as to call
Assembly.GetExe cutingAssembly( ).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 MarshalByRefObj ect 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
AssemblyFileNam e 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(str ing contentPath)
{
DirectoryInfo di = new DirectoryInfo(c ontentPath);
Assembly CurrentAssembly = Assembly.GetExe cutingAssembly( );
AppDomainSetup setup = new AppDomainSetup( );
setup.Applicati onBase = AppDomain.Curre ntDomain.BaseDi rectory;
setup.PrivateBi nPath = di.Name;
setup.Applicati onName = this.productID;
this.ad = AppDomain.Creat eDomain(this.pr oductID, null, setup);
this.aw = (AssemblyWrappe r)ad.CreateInst anceFromAndUnwr ap("TestLib"
, "TestLib.Assemb lyWrapper");

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.CreateInstan ceFromAndUnwrap 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*******@disc ussions.microso ft.com> wrote in message
news:73******** *************** ***********@mic rosoft.com...
Arrgh. I'm such a lamer. I changed the code so as to call
Assembly.GetExe cutingAssembly( ).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 MarshalByRefObj ect 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
AssemblyFileNam e 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(str ing contentPath)
{
DirectoryInfo di = new DirectoryInfo(c ontentPath);
Assembly CurrentAssembly = Assembly.GetExe cutingAssembly( );
AppDomainSetup setup = new AppDomainSetup( );
setup.Applicati onBase = AppDomain.Curre ntDomain.BaseDi rectory;
setup.PrivateBi nPath = di.Name;
setup.Applicati onName = this.productID;
this.ad = AppDomain.Creat eDomain(this.pr oductID, null, setup);
this.aw = (AssemblyWrappe r)ad.CreateInst anceFromAndUnwr ap("TestLib"
, "TestLib.Assemb lyWrapper");

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.CreateInstan ceFromAndUnwrap 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
4711
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 open project) into the forms designer. The project must be compiled for making this work. Now after recompile I need to update the user control with the newly compiled version (maybe something has changed). And THAT
9
4496
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 "Let Users Add Functionality to Your .NET Applications with Macros and Plug-Ins" article at MSDN for the dynamic loading of DLLs http://msdn.microsoft.com/msdnmag/issues/03/10/Plug-Ins/default.aspx
5
2060
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 have yet to receive any replies. If someone could please give me further information I would greatly appreciate it. Sincerely, Jon
6
4503
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, but I've just started playing around with separate AppDomains and I'm finding that I'm not having problems where I expected I would, so maybe someone can help me understand a bit better. I've read that objects instantiated in separate AppDomains...
2
309
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 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...
1
1747
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. My code is: AppDomain a = AppDomain.CreateDomain("TestDomain"); a.Load("AppDomainTestAssembly"); and in the Console window I get the following when the second line executes:
2
2646
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 recurse into an assembly to discover the referenced assemblies without actually loading the root assembly where I start the load? For example: assembly 1 contains references to assembly 1_1 and assembly 1_2 assembly 1_1 contains reference to...
2
2050
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 unload the AppDomain to release the lock on the assemly files (so to I can compile the code again if it has been modified). However, I've encountered a problem, whereby the assembly is also loaded in the default AppDomain! I have a few classes...
1
2241
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 fine, until this default.aspx uses code in a dependent assembly (say CustomAssembly). Then ASP.NET cannot find the class "CustomAssembly.MyClass" and files to compile the default.aspx. I've tried to do a AppDomain.CurrentDomain.Load(byte...
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.