473,731 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assembly.Load problems

Hey all,

I'm working on this project where I'm dynamically loading an assembly.
Basically what I want is that I can just replace my old dll file with a new
one without having to do anything else.
So to test this, I did the following: I temporarily tested it by saving my
dll file in C:\, so let's say I have C:\dynamic.dll.

I have created a winforms application with a simple button that calls the
method with Assembly.Load, which you can see here (notice a lot of debug
statement to try and find the problem):

public MyClass getClass(Domain domain)
{
AssemblyName assemblyName = new AssemblyName();
Assembly classAssembly = null;
try
{
assemblyName.Co deBase = @"C:\dynamic.dl l";
System.Diagnost ics.Debug.Write Line("Codebase for dynamic dll set to: "
+ assemblyName.Co deBase);
classAssembly = Assembly.Load(a ssemblyName);
System.Diagnost ics.Debug.Write Line("Loaded dynamic.dll, returning
instance...");
return (MyClass)classA ssembly .CreateInstance ("MyClass2", false,
System.Reflecti on.BindingFlags .CreateInstance , null, null, null, new
Object[]{domain});
}
catch(Exception ex) {
System.Diagnost ics.Debug.Write Line("Exception : " + ex.Message); }
finally {
classAssembly = null; assemblyName =
null;System.Dia gnostics.Debug. WriteLine("Clea ning up"); }
return null;
}

So, now comes the problem. When I test this the first time, all is well. All
debug statements are printed and my output furthermore shows ''test.exe':
Loaded 'c:\dynamic.dll ', Symbols loaded.' to show it actually loaded the
assembly.

However when I rename my dynamic.dll to bogus.dll and click the button again
(without closing the application) it won't throw an exception 'dynamic.dll
not found' or something, which I was expecting. It does show all debug
statements again, but nothing else. I first thought it was still in memory
or something, therefor I added the try-catch-finally block in my getClass
method and set my objects to null (but as I was expecting, that didn't
help).

So, is there more to this then meets the eye? Can I / Do I have to unload
the assembly somehow? Why doesn't it give the error when renaming my dll so
it shouldn't be found?

Thanks! Regards,

Razzie

Nov 16 '05 #1
2 5337
Hey Razzie,

Once you've loaded an assembly to the AppDomain, you cannot unload the
assembly - you have to unload the entire domain.
Therefore, if you need to reload an assembly 'on the fly', you have to use a
dedicated AppDomain. I have gone through all this hell while working on
X-Unity, so if you are stuck with some problem - post it here and hopefully
I notice your message and reply :-)

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Razzie" <ra****@quickne t.nl> wrote in message
news:eT******** ******@tk2msftn gp13.phx.gbl...
Hey all,

I'm working on this project where I'm dynamically loading an assembly.
Basically what I want is that I can just replace my old dll file with a
new one without having to do anything else.
So to test this, I did the following: I temporarily tested it by saving my
dll file in C:\, so let's say I have C:\dynamic.dll.

I have created a winforms application with a simple button that calls the
method with Assembly.Load, which you can see here (notice a lot of debug
statement to try and find the problem):

public MyClass getClass(Domain domain)
{
AssemblyName assemblyName = new AssemblyName();
Assembly classAssembly = null;
try
{
assemblyName.Co deBase = @"C:\dynamic.dl l";
System.Diagnost ics.Debug.Write Line("Codebase for dynamic dll set to:
" + assemblyName.Co deBase);
classAssembly = Assembly.Load(a ssemblyName);
System.Diagnost ics.Debug.Write Line("Loaded dynamic.dll, returning
instance...");
return (MyClass)classA ssembly .CreateInstance ("MyClass2", false,
System.Reflecti on.BindingFlags .CreateInstance , null, null, null, new
Object[]{domain});
}
catch(Exception ex) {
System.Diagnost ics.Debug.Write Line("Exception : " + ex.Message); }
finally {
classAssembly = null; assemblyName =
null;System.Dia gnostics.Debug. WriteLine("Clea ning up"); }
return null;
}

So, now comes the problem. When I test this the first time, all is well.
All debug statements are printed and my output furthermore shows
''test.exe': Loaded 'c:\dynamic.dll ', Symbols loaded.' to show it actually
loaded the assembly.

However when I rename my dynamic.dll to bogus.dll and click the button
again (without closing the application) it won't throw an exception
'dynamic.dll not found' or something, which I was expecting. It does show
all debug statements again, but nothing else. I first thought it was still
in memory or something, therefor I added the try-catch-finally block in my
getClass method and set my objects to null (but as I was expecting, that
didn't help).

So, is there more to this then meets the eye? Can I / Do I have to unload
the assembly somehow? Why doesn't it give the error when renaming my dll
so it shouldn't be found?

Thanks! Regards,

Razzie


Nov 16 '05 #2
'This hell'? Lol that sounds like it may be too much work for my project!
Anyway, I'll sure take a look at it as soon as I can. Thanks!

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Hey Razzie,

Once you've loaded an assembly to the AppDomain, you cannot unload the
assembly - you have to unload the entire domain.
Therefore, if you need to reload an assembly 'on the fly', you have to use
a dedicated AppDomain. I have gone through all this hell while working on
X-Unity, so if you are stuck with some problem - post it here and
hopefully I notice your message and reply :-)

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Razzie" <ra****@quickne t.nl> wrote in message
news:eT******** ******@tk2msftn gp13.phx.gbl...
Hey all,

I'm working on this project where I'm dynamically loading an assembly.
Basically what I want is that I can just replace my old dll file with a
new one without having to do anything else.
So to test this, I did the following: I temporarily tested it by saving
my dll file in C:\, so let's say I have C:\dynamic.dll.

I have created a winforms application with a simple button that calls the
method with Assembly.Load, which you can see here (notice a lot of debug
statement to try and find the problem):

public MyClass getClass(Domain domain)
{
AssemblyName assemblyName = new AssemblyName();
Assembly classAssembly = null;
try
{
assemblyName.Co deBase = @"C:\dynamic.dl l";
System.Diagnost ics.Debug.Write Line("Codebase for dynamic dll set to:
" + assemblyName.Co deBase);
classAssembly = Assembly.Load(a ssemblyName);
System.Diagnost ics.Debug.Write Line("Loaded dynamic.dll, returning
instance...");
return (MyClass)classA ssembly .CreateInstance ("MyClass2", false,
System.Reflecti on.BindingFlags .CreateInstance , null, null, null, new
Object[]{domain});
}
catch(Exception ex) {
System.Diagnost ics.Debug.Write Line("Exception : " + ex.Message); }
finally {
classAssembly = null; assemblyName =
null;System.Dia gnostics.Debug. WriteLine("Clea ning up"); }
return null;
}

So, now comes the problem. When I test this the first time, all is well.
All debug statements are printed and my output furthermore shows
''test.exe': Loaded 'c:\dynamic.dll ', Symbols loaded.' to show it
actually loaded the assembly.

However when I rename my dynamic.dll to bogus.dll and click the button
again (without closing the application) it won't throw an exception
'dynamic.dll not found' or something, which I was expecting. It does show
all debug statements again, but nothing else. I first thought it was
still in memory or something, therefor I added the try-catch-finally
block in my getClass method and set my objects to null (but as I was
expecting, that didn't help).

So, is there more to this then meets the eye? Can I / Do I have to unload
the assembly somehow? Why doesn't it give the error when renaming my dll
so it shouldn't be found?

Thanks! Regards,

Razzie

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4708
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
1
11975
by: Luis Pinho | last post by:
Hi There, I've got a server that is waiting for requests, these request correspond to calls to objects that are specified in assemblies stored in the GAC. To do this, I use reflection to call the methods. Now, when I'm trying to optimize the code, I noticed that the assemblies that are loaded during the reflection call, are never
2
20920
by: Stelios | last post by:
Does anyone know how to dynamically load an assembly from a location other that the working directory by using Assembly load method. It is important to use the specific method because is the method used by CLR to enforce version and security checking. I have try by changing the appdomain APPBASE property but is not working and i am wondering weather I am doing something wrong. The code I use is the following
4
1824
by: Aashish Patil | last post by:
Hi, My code is trying to load an assembly using Assembly.Load(string assemblyName); Assume that the current executing assembly is called Test.dll The following code works fine
14
14270
by: Nak | last post by:
Hi there, Does anyone know how I would get the value of the assembly GUID in code from within the same application? Thanks in advance. Nick. -- /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Slow internet connection?
1
2900
by: lgbjr | last post by:
Hi All, I am moving some of my MDI Child forms to DLLs. The first 3 forms worked fine. But the 4th one is causing me some grief! I use the following code to load a form from a DLL: Dim asmAssemblyContainingForm As = .LoadFrom("neiconv.dll") Dim TypeToLoad As Type = asmAssemblyContainingForm.GetType("neiconv.Frm_ND")
6
44488
by: Steve | last post by:
I'm playing with late binding and trying a very simple test to load an assembly In my "Host" application I have this code: <code> string modulePath = @"C:\PMDRepository\Tools\ManfBusProcMgr\Modules\TestModule\bin\Debug\TestModule"; Assembly a = Assembly.Load(modulePath); </code>
1
2322
by: Erland | last post by:
Hi all, As per my understanding in order to load an assembly using Assembly.Load() you have to provide fully qualified name of the assembly you are trying to load e.g. Assembly asmb=Assembly.Load("System.Windows.Forms,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"); In this case i will have to provide the whole big string in
12
1715
by: Ron M. Newman | last post by:
Hi, I can load an assembly using the Assembly.Load(....) However, I'd like dynamic loading of assemblies to be identical to putting an assembly reference in your VS2005 project. and yes, I know about the unloading problems. Let's say I don't care for the moment. If I have an assemlbly file at "c:\\assembly.dll" - how do I load it at runtime as if it were references in the project at build time? I know
0
8944
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
8772
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9443
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
9233
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,...
0
8184
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6732
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
4803
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3258
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
2714
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.