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.CodeBase = @"C:\dynamic.dll";
System.Diagnostics.Debug.WriteLine("Codebase for dynamic dll set to: "
+ assemblyName.CodeBase);
classAssembly = Assembly.Load(assemblyName);
System.Diagnostics.Debug.WriteLine("Loaded dynamic.dll, returning
instance...");
return (MyClass)classAssembly .CreateInstance("MyClass2", false,
System.Reflection.BindingFlags.CreateInstance, null, null, null, new
Object[]{domain});
}
catch(Exception ex) {
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); }
finally {
classAssembly = null; assemblyName =
null;System.Diagnostics.Debug.WriteLine("Cleaning 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 2 5317
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****@quicknet.nl> wrote in message
news:eT**************@tk2msftngp13.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.CodeBase = @"C:\dynamic.dll"; System.Diagnostics.Debug.WriteLine("Codebase for dynamic dll set to: " + assemblyName.CodeBase); classAssembly = Assembly.Load(assemblyName); System.Diagnostics.Debug.WriteLine("Loaded dynamic.dll, returning instance..."); return (MyClass)classAssembly .CreateInstance("MyClass2", false, System.Reflection.BindingFlags.CreateInstance, null, null, null, new Object[]{domain}); } catch(Exception ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); } finally { classAssembly = null; assemblyName = null;System.Diagnostics.Debug.WriteLine("Cleaning 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
'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.com> wrote
in message news:%2****************@TK2MSFTNGP15.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****@quicknet.nl> wrote in message news:eT**************@tk2msftngp13.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.CodeBase = @"C:\dynamic.dll"; System.Diagnostics.Debug.WriteLine("Codebase for dynamic dll set to: " + assemblyName.CodeBase); classAssembly = Assembly.Load(assemblyName); System.Diagnostics.Debug.WriteLine("Loaded dynamic.dll, returning instance..."); return (MyClass)classAssembly .CreateInstance("MyClass2", false, System.Reflection.BindingFlags.CreateInstance, null, null, null, new Object[]{domain}); } catch(Exception ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); } finally { classAssembly = null; assemblyName = null;System.Diagnostics.Debug.WriteLine("Cleaning 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
|
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.
--...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |