14 September 2010 - 12:40 AM / by Dominic Pettifer. 2 Comments for How to get a Reference to all Assemblies in the \bin Folder.
Technical Article - Something I’ve been stuck on for a while is a reliable way to get a list of all Assemblies (DLL files) that are located in the ASP.NET website’s \bin folder. I’ve frequently needed this for IOC style Controller registration code (find all types in all Assemblies that implement IController for instance), or for my ASP.NET Bootstrapper pattern (find all types that implement IBootstrapperTask). Relying on AppDomain.CurrentDomain.GetAssemblies () can be dangerous as I’ll discuss.
If you look at my previous blog posts on IOC containers and the Bootstrapper pattern, you’ll find that I’ve frequently had a need to locate all types in all assemblies that implement a certain interface, or derive from a particular abstract base class (such as IController for an IOC powered Controller factory, or IBootstrapperTask for the Bootstrapper). The idea is that the caller has maximum flexibility over where they put these classes, the ControllerFactory or Bootstrapper will just find them.
Frequently I’ve run into the problem of how to get a list of all these Assemblies. There are a number of ways to get references to currently loaded Assemblies such as:
Assembly.GetExecutingAssembly();
...that gets the Assembly the currently executing code resides in, or:
Assembly.GetCallingAssembly();
...that gets the Assembly that called the code that is currently executing.
To get all Assemblies you might be tempted to write:
IList<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies();
...which gets all "currently loaded" Assemblies. However, this is dangerous because of the key phrase "currently loaded". The .NET runtime will load assemblies on demand as code from those assemblies is executed. If I’m trying to find all IBootstrapperTask or IController types in some Application_Start code, the assemblies that contain the necessary types might not have been loaded yet, so we’ll be missing some Tasks or Controllers.
I was tripped up on this, as some strange .NET runtime behaviour meant AppDomain.CurrentDomain.GetAssemblies() returned all assemblies when the application was first run, as I wanted. But after the first AppDomain restart, the normal .NET runtime dynamic assembly loading behaviour kicked in. It failed to load the assemblies containing the bootstrapper tasks and MVC Controllers, and my ASP.NET MVC application, that I deployed live, promptly crashed, doh!
I looked through the ASP.NET MVC 2.0 source code and looked up how AreaRegistration.RegisterAllAreas(); is implemented. This line is usually put into the Global.asax Application_Start() method for a brand new MVC app, and internally it scans all assemblies for types that implement the AreaRegistration abstract type, for registering Areas. This is exactly the behaviour I'm after.
It appears RegisterAllAreas() makes a call to BuildManager.GetReferencedAssemblies(). Well, if it's good enough for MVC then it's good enough for me :-)
I did some experimentation and BuildManager.GetReferencedAssemblies() doesn’t suffer from the same dynamic assembly loading problem. It will even pick up adhoc, random DLL's dropped into the \bin folder, even with no references to any projects in the Visual Studio solution. So it appears far more reliable than AppDomain.CurrentDomain.GetAssemblies().
So we have a way to get a reference to all assemblies and avoid the dreaded dynamic assembly loading problem, but what about getting a reference to just assemblies in the \bin folder. I wrote a handy AssemblyLocator utility function:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
namespace MvcLibrary.Ioc
{
public static class AssemblyLocator
{
private static readonly ReadOnlyCollection<Assembly> AllAssemblies = null;
private static readonly ReadOnlyCollection<Assembly> BinFolderAssemblies = null;
static AssemblyLocator()
{
AllAssemblies = new ReadOnlyCollection<Assembly>(
BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList());
IList<Assembly> binFolderAssemblies = new List<Assembly>();
string binFolder = HttpRuntime.AppDomainAppPath + "bin\\";
IList<string> dllFiles = Directory.GetFiles(binFolder, "*.dll",
SearchOption.TopDirectoryOnly).ToList();
foreach (string dllFile in dllFiles)
{
AssemblyName assemblyName = AssemblyName.GetAssemblyName(dllFile);
Assembly locatedAssembly = AllAssemblies.FirstOrDefault(a =>
AssemblyName.ReferenceMatchesDefinition(a.GetName(), assemblyName));
if (locatedAssembly != null)
{
binFolderAssemblies.Add(locatedAssembly);
}
}
BinFolderAssemblies = new ReadOnlyCollection<Assembly> (binFolderAssemblies);
}
public static ReadOnlyCollection<Assembly> GetAssemblies()
{
return AllAssemblies;
}
public static ReadOnlyCollection<Assembly> GetBinFolderAssemblies()
{
return BinFolderAssemblies;
}
}
}You can use this in the IocControllerFactory like so:
public class IocControllerFactory : DefaultControllerFactory
{
public IocControllerFactory()
{
Type controllerInterfaceType = typeof(IController);
List<Type> controllerTypes = new List<Type>();
foreach (Assembly assembly in AssemblyLocator.GetBinFolderAssemblies ())
{
// Rest of ControllerFactory class, see blog post (snip)...or in the Bootstrapper class:
public class Bootstrapper
{
static Bootstrapper()
{
Type bootStrapperType = typeof(IBootstrapperTask);
List<Type> tasks = new List<Type>();
foreach (Assembly assembly in AssemblyLocator.GetBinFolderAssemblies())
{
// Rest of Bootstrapper class, see blog post (snip)
Nice. Workaround can be to reload assemblies to AppDomain "by hand" on application startup. But BuildManager seems to be exactly THE mechanism that adressed problem like this.
Btw, why not use HttpRuntime.BinDirectory instead of HttpRuntime.AppDomainAppPath + "bin\\"?
Posted on 1 October 2010 - 8:05 PM / by Vasilio
Good advice on using HttpRuntime.BinDirectory instead of HttpRuntime.AppDomainAppPath + "bin\\". I'll certainly change my code to use this.
Posted on 2 October 2010 - 4:25 PM / by Dominic Pettifer (Administrator)
<b><a href="http://www.gucciitaly.net/">cheap gucci</a></b> s, and <b><a href="http://www.gucciitaly.net/">gucci outlet</a></b> gradually erode, causing nerve <b><a href="http://www.gucciitaly.net/">cheap gucci</a></b> damage, these important blood vessels, nerve <b><a href="http://www.gucciitaly.net/">gucci for sale</a></b> surgery <b><a href="http://www.gucciitaly.net/">discount gucci</a></b> must b...
Posted on 15 October 2011 - 2:53 AM / by gucci outlet
Thank you, it’s very inspiring description about this good topic, it might be very helpful.
Posted on 25 January 2012 - 4:21 PM / by custom writing
/p>
<p> <strong><a href="http://www.monclersjackor.net/" title="moncler väst">moncler väst</a></strong> coat down jacket, Canada goose, are trying <strong><a href="http://www.monclersjackor.net/moncler-boots-c-9.html" title="Moncler Boots">Moncler Boots</a></strong> to attract consumers. <strong><a href="http://www.monclersjackor.net/moncler-coats-m%C3%A4n-c-2.html" title="Moncler outlet">Moncler outlet</a></strong> </P>
<p> However, still <strong><a href="http://www.monclersjackor.net/" title="Moncler jackor">Moncler jackor</a></strong> now, fashiona design <strong><a href="http://www.monclersjackor.net/moncler-barn-c-7.html" title="Moncler Barn">Moncler Barn</a></strong> ideal and comfortable feel, M...
Posted on 16 October 2011 - 3:14 AM / by Moncler jacka
So we have a way to get a reference to all assemblies and avoid the dreaded dynamic assembly loading problem, but what about getting a reference to just assemblies in the \bin folder. I wrote a handy AssemblyLocator utility function:
Posted on 14 January 2012 - 8:22 PM / by Haus-Kataloge
[url=http://www.toryburch4outlets.com/]Tory Burch Outlet[/url] awesome to chat with other bloggers, and I also bonded with the TB Marketing intern over how much we adored the [url=http://www.toryburch4outlets.com/tory-burch-wedges-c-6.html]Tory Burch Wedges[/url]. We�re both really into versatility at the moment, and you can wear this as a large clutch, a folded-over minimessenger, or an unfolded large messenger. If you�re as much a fan as we are, [url=http://www.toryburch4outlets.com/tory-burch-heels-c-5.html]Tory Burch Heels[/url] you can pick it up via toryburchnewshoes.com.
Posted on 10 December 2011 - 8:18 AM / by Tory Burch Outlet
rning is always the point, not because we will not <a href="http://www.usjordanstore.com/air-jordan-retro-7-C38.html" title="Air Jordan Retro 7 Sneaker">Air Jordan Retro 7 Sneaker</a> do, but we have no intention to get there. Thursdays inspection to prove that we are powerful, able to improve health, as long as we try to do, no <a href="http://www.usjordanstore.com/" title="jordans on sale">jordans on sale</a> matter the outcome, it is best, I hope our class can break the record next week, for a day no point...
Posted on 19 May 2011 - 3:10 AM / by Jordan Shoes
Besides, we had <a href="http://www.mulberryonsale.co.uk/" title="Mulberry Bags">mulberry bags</a> shows in some shopping malls which I think was a very good way to practice oral <a href="http://www.mulberryonsale.co.uk/" title="Mulberry Sale">mulberry sale</a> and <a href="http://www.mulberryonsale.co.uk/" title="Mulberry UK">mulberry uk</a>. What's more? Our teacher told us to circularly play tape recorder of any kind of <a href="http://www.mulberryonsale.co.uk/" title="Mulberry Bags UK">mulberry bags uk</a> as soon as we get up every morning. I did what <a href="http://www.mulberryonsale.co.uk/products_new.html" title="Mulberry Bags 2011">mulberry bags 2011 </a> told and it was so surprise for me to find one day I can speak out those from the recorder unconsciously.
Posted on 28 June 2011 - 1:57 AM / by Mulberry Bags Sale
<a href="http://www.louisvuihhttonoutletbbh.com">louis vuitton</a>
[url=http://www.louisvuissttonoutletbbh.com]louis vuitton[/url]
Posted on 1 December 2011 - 11:52 AM / by coach
<strong><a href="http://www.cheapnikeshoxsneakers.com/nike-shox-tl3-shoes-c-13.html" title="Nike Shox TL3 Shoes">Nike Shox TL3 Shoes</a></strong> s of individuals, it <strong><a href="http://www.cheapnikeshoxsneakers.com/nike-shox-tl1-shoes-c-16.html" title="Nike Shox TL1 Shoes">Nike Shox TL1 Shoes</a></strong> seems to <strong><a href="http://www.cheapnikeshoxsneakers.com/nike-shox-tl1-shoes-c-16.html" title="Shox TL1">Shox TL1</a></strong> have thousands of people to her distant blessing. </P> <br> <P> heard later, <strong><a href="http://www.cheapnikeshoxsneakers.com/" title="nike shox nz">nike shox nz</a></strong> that is, that once the audience in that intelligence, lying three bloody people. </P> <strong><a href="http://www.cheapnikeshoxsneakers.com/nike-shox-tl3-shoes-c-13.html" title="Nike Shox TL3">Nike Shox TL3</a></strong> <br> <P> no more than the </P> <br> <P> beyond the <strong><a href="http://www.cheapnikeshoxsneakers.com/nike-shox-tl1-shoes-c-16.html" title="Nike Shox TL1">Nike Shox TL1</a></strong> horror of death, I am afraid to sleep or loss of stop... <strong><a href="http://www.cheapnikeshoxsneakers.com/nike-shox-tl1-shoes-c-16.html" title="Cheap Nik">Cheap Nik</a></strong>
Posted on 2 February 2012 - 5:55 AM / by cheap nike shox
d by my own, Franklin and Marshall my own hands it Franklin Marshall Greece is abandoned, so as Hanhen always irreparable. As a result, the original is not surprising that a small stone, has indeed become a jewel Franklin Marshall Outlet of my heart
Posted on 21 February 2012 - 3:26 AM / by Franklin Marshall
Thanks for the comments guys......
Posted on 14 September 2011 - 10:34 AM / by Hotel Management Software
Newest styles of <a href="http://www.christianlouboutinreplicacl.com/"><strong>christian louboutin high heels</strong></a> in hot sale now, <a href="http://www.christianlouboutinreplicacl.com/"><strong>Christian Louboutin Knockoffs</strong></a> shoes sale now, buy <a href="http://www.christianlouboutinreplicacl.com/"><strong>christian louboutin replica</strong></a> in our online uk store .your shoes sales prices will save.
Posted on 22 September 2011 - 2:36 AM / by christian louboutin replica
Interesting comments. And they are moderated?:))
Posted on 23 September 2011 - 3:55 PM / by eugene
<b><a href="http://www.myuggbootssaleuk.com/">ugg uk</a></b> t orphans, but also to <b><a href="http://www.myuggbootssaleuk.com/">ugg boots uk</a></b> <b><a href="http://www.myuggbootssaleuk.com/">ugg boots sale uk</a></b> bear the <b><a href="http://www.ugg-ukboots.co.uk/">ugg boots uk</a></b> <b><a href="http://www.muggboots.com/">ugg boots</a></b> grief <b><a href="http://www.imcheapuggboots.com/">uggs</a></b> of the <b><a href="http://www.ugg-ukboots.co.uk/">ugg boots sale uk</a></b> <b><a href="http://www.ugg-ukboots.co.uk/">ugg boots</a></b> orphans and <b><a href="http://www.imcheapuggboots.com/">cheap ugg boots</a></b> the <b><a href="http://www.imcheapuggboots.com/">cheap uggs</a></b> more spiritual suffering. Fortunatel...
Posted on 16 November 2011 - 6:28 AM / by ugg boots sale
get Abercrombie online shop I was a Abercrombie Zürich lost age of five. I always think Abercrombie and Fitch of it from time to time, in my mind, its mellow and beautiful it is no substitute for a. Especially since the fault was ca
Posted on 21 February 2012 - 3:25 AM / by Abercrombie Zürich
<P>
<b><a href=http://www.nzhairstraightener.org>GHD</a></b></P>
<P>
<b><a href=http://www.nzhairstraightener.org>GHD Hair</a></b></P>
<P>
<b><a href=http://www.nzhairstraightener.org>GHD Straighteners</a></b></P>
<P>
<b><a href=http://www.nzhairstraightener.org>Ghd Hair Straighteners</a></b></P>
<P>
<b><a href=http://www.nzhairstraightener.org>ghd NZ</a></b></P>
<P>
<b><a href=http://www.drebeats-headphones.com>Dr Dre Beats</a></b></P>
<P>
<b><a href=http://www.drebeats-headphones.com>Dr Dre Headphones</a></b></P>
<P>
<b><a href=http://www.drebeats-headphones.com>Beats Headphones</a></b></P>
<P>
<b><a href=http://www.drebeats-headphones.com>Dre Beats</a></b></P>
<P>
<b><a href=http://www.drebeats-headphones.com>Dre Headphones</a></b></P>
<P>
<b><a href=http://www.billig-rettetang.com>ghd norge</a></b></P>
<P>
<b><a href=http://www.billig-rettetang.com>ghd rettetang</a></b></P>
<P>
<b><a href=http://www.billig-rettetang.com>ghd rettetang billig</a></b></P>
Posted on 18 November 2011 - 2:28 AM / by ghd
because because <i><a href=http://www.goodjordansale.com/air-jordan-4-c-17.html>jordan retro 4</a></i> because he knew once he got to the ER he wasn't getting any
food, Wagensomer said.Watch What You EatMost physicians
Posted on 2 December 2011 - 6:02 AM / by Jordan Retros
Hi I am a big reader, would really appreciate an invite. Your blog is very nice and I really enjoy the style and content. THanks
Posted on 30 December 2011 - 5:59 AM / by t shirt printing uk
years, I have gone Ralph Lauren Polo through many places, experienced many things, seen a lot of stone, the family has collected a lot of beautiful or strange ore, Billige Ralph Lauren but no one can Instead, Ralph Lauren Danmark I can f
Posted on 21 February 2012 - 3:24 AM / by ralph lauren polo
HttpApplication context pooling with multiple requests (from the blog IHttpModule Gotchas – The Init() Method Can Get Called Multiple Times )
“Success is not the key to happiness. Happiness is the key to success.” - http://t.co/8k1uOWyR
about 15 hours ago from webDisappointed that Google's doodle is just a .gif. They could do that in HTML5 surely?
about 18 hours ago from webHave a look at http://t.co/R8tu6VBV - 90% of the website is an advert, ridiculous. Need to start using IMDB / Metacritic a lot more.
3:41 PM February 21st from web"If you see a proprietary memory card, they blew it." - http://t.co/qifZsKPs
1:28 PM February 21st from web