Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2005

Tip: Looking for answers? Try searching our database.

Code isolation with the AppDomain class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon Shemitz - 17 Apr 2005 04:57 GMT
I am responsible for the plugin-loading part of an app. One of the
things the loader has to do is check licensing, and unload any
unlicensed plugins.

I naively did this as:

 AppDomain Sandbox = AppDomain.CreateDomain("Sandbox");
 try
 {
   Assembly Suspect = Sandbox.Load(PluginName);
   // do some tests on Suspect.GetExportedTypes()
 }
 finally
 {
   AppDomain.Unload(Sandbox);
 }

The code seemed to work, but it turns out that
Sandbox.Load(PluginName) is actually loading the PluginName into both
the Sandbox AppDomain and the default AppDomain. When I unload
Sandbox, I'm left with all tested plugins still loaded into the
default AppDomain, whether they are licensed or not.
   
I do NOT want to execute any assemblies: I just want to load my
suspects; do some tests in the temp AppDomain; and return the test
results to the default AppDomain.

It looks like my only option is to use AppDomain.DoCallBack to execute
my tests, and use SetData to store the results. Is there a better way?

Signature

www.midnightbeach.com

David Levine - 17 Apr 2005 12:16 GMT
The reason why the plugin is getting loaded into both appdomains is because
you are loading the plugin in the context of the default appdomain. You need
to write a class that will remotely load the plugin in the context of the
2nd appdomain and then return the results of the license check back to the
default appdomain.

The statement that is causing the problem is
Assembly Suspect = Sandbox.Load(PluginName);

An assembly object is passed by value, which causes the assembly to get
loaded in both the 2nd appdomain and the one in which the Assembly object is
returned to, in this case the default.

You need to create a class derived from MarshalByRefObject, create an
instance of that in the remote appdomain to which you get back a reference
to a transparent proxy, and then call a method on that object that performs
the test. This link describes this fairly well..

http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx

>I am responsible for the plugin-loading part of an app. One of the
> things the loader has to do is check licensing, and unload any
[quoted text clipped - 25 lines]
> It looks like my only option is to use AppDomain.DoCallBack to execute
> my tests, and use SetData to store the results. Is there a better way?
Jon Shemitz - 17 Apr 2005 20:49 GMT

> The reason why the plugin is getting loaded into both appdomains is because
> you are loading the plugin in the context of the default appdomain. You need
[quoted text clipped - 4 lines]
> The statement that is causing the problem is
> Assembly Suspect = Sandbox.Load(PluginName);

Yes, I know that, thanks.

> You need to create a class derived from MarshalByRefObject, create an
> instance of that in the remote appdomain to which you get back a reference
> to a transparent proxy, and then call a method on that object that performs
> the test. This link describes this fairly well..

Ah, yes. That looks much better than using DoCallBack. Thanks.

Signature

www.midnightbeach.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.