Los Techies : Blogs about software and anything tech!

Dynamic Loading of .Net Assemblies


Problem: You have dependencies which can be in a number of directories outside of the directory your app is running in. DotNet AppDomains are heavy to say the least and very high in ceremony.  You do not want to deal with secondary app domain creation.

Solution: Override  AssemblyResolve event on your current app domain.

 

    public class AppDomainLoader : IAppDomainLoader
    {
       
        private ResolveEventHandler _searchAssemblyMethod = _searchDir;
        public ResolveEventHandler FindAssemblyMethod
        {
            set { _searchAssemblyMethod = value}
        }
        private static string[] _directoriesToSearch;
        private  static Assembly _searchDir(Object o, ResolveEventArgs e)
        {
            foreach (var s in _directoriesToSearch)
            {
                var filetoload = s +  e.Name.Split(',')[0] + ".dll"//bad one liner that parses the first part of the assembly name and adds .dll at the end
                if (File.Exists(filetoload)) //sees if the file is even in the directory
                {
                   return Assembly.LoadFrom(filetoload);  //returns assembly once found, a bit naive and does not handle version conflicts well.
                }
            }
            return null;
          
        }
     
        public void Search(string[] directoriesToSearch)
        {
            _directoriesToSearch = directoriesToSearch; //sets directories to search to allow entry point for dynamic searching
            AppDomain.CurrentDomain.AssemblyResolve -= _searchAssemblyMethod; //cleans up previous events, may not be needed.
            AppDomain.CurrentDomain.AssemblyResolve += _searchAssemblyMethod; //registered the event

        }
    }

Now whenever there is a missing Assembly, the _searchAssemblyMethod will be fired,  and all directories given to the Search method will attempt to find an assembly with the matching name.

Kick It on DotNetKicks.com
Posted Aug 31 2009, 06:29 PM by Ryan Svihla
Filed under: , ,

Comments

DotNetShoutout wrote Dynamic Loading of .Net Assemblies - Ryan Svihla - Los Techies
on 09-01-2009 1:25 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Richard Dingwall wrote re: Dynamic Loading of .Net Assemblies
on 09-01-2009 6:42 AM

This trick is also good for including assemblies as embedded resources in WPF apps, where ilmerge doesn't work.

Dynamic Loading of .Net Assemblies « Jasper Blog wrote Dynamic Loading of .Net Assemblies « Jasper Blog
on 09-02-2009 2:36 AM

Pingback from  Dynamic Loading of .Net Assemblies « Jasper Blog

Reflective Perspective - Chris Alcock » The Morning Brew #424 wrote Reflective Perspective - Chris Alcock » The Morning Brew #424
on 09-02-2009 3:26 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #424

igorbrejc.net » Fresh Catch For September 7th wrote igorbrejc.net » Fresh Catch For September 7th
on 09-07-2009 9:03 AM

Pingback from  igorbrejc.net » Fresh Catch For September 7th

Arnis L. wrote re: Dynamic Loading of .Net Assemblies
on 09-08-2009 9:33 AM

I might sound stupid - but isn't this the same what assembly probing provides? If so - are there any difference why this approach is supposed to be preferable?

Ryan Svihla wrote re: Dynamic Loading of .Net Assemblies
on 09-08-2009 10:25 AM

@Arnis This is for when the default assembly probing can't work easily. I've implemented this for configurable plugin architectures and most recently with a command line client for my BDD project SpecMaker.  

In all those cases the consumer of software was passing in locations to "search" for assemblies.  SpecMaker.exe originally was relying on default assembly loading and this required it to be run where all assemblies needed were already placed next to it.  This was a non-issue for most but some people didn't A) want to have to copy specmaker to their build directory or B) have a spread out build structure where different components are located in different side by side directories.

thanks again for the feedback and I hope that answers your questions.

Ryan Svihla wrote re: Dynamic Loading of .Net Assemblies
on 09-08-2009 10:26 AM

@Arnis to answer to your second question, this is not preferable to assembly probing itself, its more like extending it.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems