Another Reflection Question?

Another Reflection Question?

Old forum URL: forums.lhotka.net/forums/t/855.aspx


RangerGuy posted on Wednesday, August 09, 2006

I just successfully used reflection to instantiate a BO and call method dynamically :) YEEPIE

Can I kill this dynamically loaded assembly as soon as I have the results from the method we invoked and how do I do this?

 

 

 

ajj3085 replied on Wednesday, August 09, 2006

Yes and no. Once the assembly is loaded, it stays in the appdomain.

The only way to do what you want would be to create another appdomain and load the assembly there.  Then when you're done, unload the appdomain.

HTH
Andy

RangerGuy replied on Wednesday, August 09, 2006

oh i just read that...was hoping there was a simpler solution :( but if that's the only way then that's the only way.

How long do these dynamically loaded assemblies have a reference to the physical dll file?

I was hoping that once they are done doing there work they are sent to the garbage collection

DavidDilworth replied on Thursday, August 10, 2006

Why do you want to unload the assembly that one of your BOs is in? 

Surely you actually want the assembly with your BOs to remain resident the whole time?

Otherwise the .NET framework will keep loading the assembly back into memory every time you access that BO!

AndrewCr replied on Thursday, August 10, 2006

No, assemblies are not garbage-collected like normal objects.  The only way to release the physical DLL is to unload the AppDomain, as described previously.  (Sorry.)

As to "why would you?": Being able to do this could be useful in updating always-on systems like services.  Say you had a service that handled a number of different types of requests, each requiring different business objects.  You would upgrade the service by disabling only the requests that used a specific business object and unloading (only) that app domain.  You could then replace the DLL and re-enable the request.  In the meantime the service continues to run and service other requests.

Of course to do this, you need to design the service from the ground up to have different AppDomains for each type of request.

Hop this helps.

RangerGuy replied on Thursday, August 10, 2006

It's a long story LOL! But what we need to do is load an assembly based on some criteria. Then invoke a method dynamically. I've accomplished what we need to do using reflection :)

But I was concerned with the assembly not releasing it's resources effectively. I have since learned the only way to release an assemblies reference to the physical file and other resources is to unload the asp.net app domain.

You can do this by "touching" the webconfig file :) 

Please keep in mind if you do this you loose all session data and cached data as well because they all are stored in the app domain.

Thanks everybody for the help! :)

ajj3085 replied on Thursday, August 10, 2006

Uh... I wouldn't recommend 'touching' the web.config just to unload appdomains.. since as you know it will cause problems for users.

Why not just proceed as recommended?  Creating an appdomain and then unloading it when you are doing isn't that difficult, and you don't need to worry about causing havock for your users.

RangerGuy replied on Thursday, August 10, 2006

mmmm.... was thinking that the need to update the app domain would not happen much after the site goes to the production environment. But if it does need to be done then the users will loose all there data that is not committed.

Well looks like I got some homework tonight LOL! I'm going to do some reading on AppDomains.

 

tymberwyld replied on Friday, August 11, 2006

Well, if you're interested I have a class that manages this for you.  It's actually very simple to understand once you've used it a few times.  It encapsulates everything including the calls through Reflection.  It can also Load and Unload AppDomains.

Basically, the way to do this is instantiate a new AppDomain yourself (this may get somewhat complicated as there are many weird options in the AppDomainSetup class).  You can then Unload that new AppDomain without affecting your current Main AppDomain running from ASP.NET.

Copyright (c) Marimer LLC