Home All Groups Group Topic Archive Search About

AssemblyLoad event not raised in second AppDomain

Author
21 Jul 2006 12:43 PM
Daniel Ellis
(This is my first ever post as I have found nothing elsewhere on this)

I have been making a test app to help me understand AppDomains and which
domains an assembly loads in.  This is really so I can unload dlls by
unloading the appdomain.

This test app registeres for all the events on the two AppDomains which
are in the app, including specifically the AssemblyLoad event.

In the primary appdomain this event is raised when creating an instance
of my plugin class within the primary appdomain.  Another event is
raised for the interface assembly.  All good so far.

However when the class is created in the second appdomain the
AssemblyLoad event never fires in the second appdomain.
(I do still get the event fired in the first appdomain but only for the
interface assembly, as expected).

If I call GetAssemblies() on the second appdomain before and after the
instantiation of the object you can clearly see the assembly has loaded
in the second appdomain.  So why was the event never raised?

(I have attached the test app solution which you can simply compile is
VS2005, if your interested!)

Is this by design?  Or is it a bug?  Any ideas?

Thanks,
Daniel Ellis.

Author
24 Jul 2006 9:22 AM
Walter Wang [MSFT]
Hi Daniel,

Thank you for your post.

Based on my understanding, your question is how to handle events of 2nd
AppDomain created from 1st AppDomain. If I've misunderstood anything,
please feel free to post here.

In your code, the appDomain2.AssemblyLoad event is being added a delegate
from current AppDomain, which means let another AppDomain calling into
current AppDomain's code, that's why the event handler doesn't work.

As far as I know, the best way to communicate between AppDomains is to use
.NET Remoting. Please feel free to post here if you need more info on how
to use .NET Remoting between AppDomains.

To handle the 2nd AppDomain's event, we need to pass an AppDomainSetup
instance with an AppDomainInitializer to AppDomain.CreateDomain():

    private static void AppDomain2_Init(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyLoad += new
AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
    }

    static void CurrentDomain_AssemblyLoad(object sender,
AssemblyLoadEventArgs args)
    {
    }

    private void btnCreateAppDomain2_Click(object sender, EventArgs e)
    {
        AppDomainInitializer appInit = new
AppDomainInitializer(AppDomain2_Init);
        AppDomainSetup ads = new AppDomainSetup();
        ads.AppDomainInitializer = appInit;
        appDomain2 = AppDomain.CreateDomain("AppDomain2", new
Evidence(AppDomain.CurrentDomain.Evidence), ads);
    }

Please note the callback passed to AppDomainInitializer must be a static
method, this is because before the new AppDomain get's initialized, there's
no way to store instance variables in the new AppDomain.

When the AppDomain2_Init() gets callbacked, it will execute in the context
of the new AppDomain, thus we can setup its AssemblyLoad event handler
using AppDomain.CurrentDomain reference.

For more information, please refer to following MSDN Library:

============
#Application Domains and Assemblies
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconapplicationdomainsassemblies.asp

If an assembly is used by mulitple domains in a process, the assembly's
code (but not its data) can be shared by all domains referencing the
assembly.

Each application domain that accesses the assembly must have a separate
copy of the static data or method, to prevent references to object in
static variables from crossing domain boundaries.
=============

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (waw***@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
25 Jul 2006 10:04 AM
Daniel Ellis
Thank you Walter for your response.  That has answered my question, I
over looked the fact that none of the events where being raised from the
second app domain.  But it certainly makes sense now.

Much appreciated,
Daniel Ellis.
Author
26 Jul 2006 1:29 AM
Walter Wang [MSFT]
Hi Daniel,

Appreciate your update and response. I am glad to hear that the problem has
been fixed. If you have any other questions or concerns, please do not
hesitate to contact us. It is always our pleasure to be of assistance.

Have a nice day!

Regards,
Walter Wang (waw***@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

AddThis Social Bookmark Button