RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Loading an assembly with dependencies 


A friend of mine (who apparently refuses to blog, but I'll link out to him anyway since he's one of my homies) is working on a new app with a “plugin” architecture. I love that kind of stuff. One of his plugins has a dependency on another assembly. Things were not working well - even when he attempted to load the dependent assemblies into the AppDomain. We hunted around to find a solution and came accross a cool new event I was not aware of before - the AssemblyResolve event.

Anyway, here's the low-down on the entire process he was doing originally (which didn't work):

  • Load the plugin assembly into the current AppDomain.
  • Check for dependencies via GetReferencedAssemblies on the newly loaded assembly.
  • Check each of the referenced assemblies by the plugin assembly against the loaded assemblies in the current AppDomain.
  • If a referenced assembly is not loaded in the current AppDomain then load the referenced assembly.
  • Create an instance of the base type in plugin assembly via an Activator <-- This failed for assemblies with dependent assemblies (that were not already loaded in the current AppDomain, so we're not talking about assemblies in the BCL or anything)

So, after some searching around, I came accross the AssemblyResolve event, which is part of the AppDomain class. This worked - and it is cool. The AssemblyResolve event occurs when the resolution of an assembly fails. The handler receives an argument of ResolveEventArgs which contains the name of the assembly that failed to resolve. The signature of the handler indicates that an assembly is the return type. Basically, all you do is resolve the assembly yourself and return the loaded assembly when the AssemblyResolve event is raised. So cool, and easy too.

I love learning new things - maybe that is why I love .NET. There is so much in the framework that there is always new things to learn. Always new things I didn't know were there before.




                   



Leave a comment below.

Comments

  1. Peli's Blog 5/18/2004 3:15 AM
    Gravatar
  2. painlessprod 7/21/2005 12:43 PM
    Gravatar
    Finally finding someone out there with the sasme type of issue
    Was wondering:
    As soon as you load the assembly into the appdomain doesn't it load the referenced assemblies immediately if the privatepaths are in the appdomain.

    My problem is I want all assemblies shadow copied including the referenced assemblies, i can't find a way for the referenced assembly to be shadow copied like the calling assembly

    any ideas

    please contact me at <name> hotmail
  3. Jeremy Larkin 10/4/2005 6:50 AM
    Gravatar
    painlessprod

    Depending on why you are shadow copying this may help

    http://csharpthings.blogspot.com/2005/10/load-assemblies-without-locking.html

    It doesn't use shadow copying, but keeps the assemblies from locking.
  4. Michael Bakker 1/15/2009 2:12 PM
    Gravatar
    Pointing me out this event on AppDomain has greatly helped me.
    I have now turned an error-prone, half-baked solution which dynamically loads assemblies and handles resolve errors, into a tight and elegant one.
    I am now even more in favor of blogging (on specific issues, which might save the dimly lit people like me a lot of rewardless wandering across the internet).
    Keep u the good work!

    Groningen, the Netherlands
  5. 4bs3r0 2/20/2009 8:15 AM
    Gravatar
    Hi,

    Sorry to comment an old post, but I have some problems with assemblies and reflection, and your post seems to fit exactly to them.

    Is it possible to have a sample of code to illustrate your solution ?

    regards.

    Cam
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:              


Sponsor

Sections