472,373 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.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

Nov 16 '05 #1
2 5231
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


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.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

Nov 16 '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...
1
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...
2
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...
4
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
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. --...
1
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...
6
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 =...
1
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...
12
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...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.