Alex Rivera | Logout

ASP.NET - AppDomain.CurrentDomain.GetAssemblies() - Assemblies missing after AppDomain restart

Asked 2010-08-23T22:34:30.137
33

I have a Bootstrapper that looks through all Assemblies in an ASP.NET MVC application to find types that implement an IBootstrapperTask interface, and then registers them with an IOC Contrainer. The idea is that you can literaly place your IBootstrapperTasks anywhere, and organise your Projects how you please.

Code for Bootstrapper:

public class Bootstrapper
{
    static Bootstrapper()
    {
        Type bootStrapperType = typeof(IBootstrapperTask);

        IList<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies();

        List<Type> tasks = new List<Type>();

        foreach (Assembly assembly in assemblies)
        {
            var types = from t in assembly.GetTypes()
                        where bootStrapperType.IsAssignableFrom(t)
                            && !t.IsInterface && !t.IsAbstract
                        select t;

            tasks.AddRange(types);
        }

        foreach (Type task in tasks)
        {
            if (!IocHelper.Container().Kernel.HasComponent(task.FullName))
            {
                IocHelper.Container().AddComponentLifeStyle(
                    task.FullName, task, LifestyleType.Transient);
            }
        }
    }

    public static void Run()
    {
        // Get all registered IBootstrapperTasks, call Execute() method
    }
}

After a full build, AppDomain.CurrentDomain.GetAssemblies() returns all Assemblies in my solution (including all the GAC one's but that doesn't bother me).

However, if the AppDomain is restarted, or I 'bounce' the Web.Config file (adding a space and saving), the static constructor is run again but when AppDomain.CurrentDomain.GetAssemblies() is called, most of the Assemblies are missing, including the one containing my IBootstrapperTask types.

How do I get around this problem? I guess I could System.IO the /bin directory and loa

Edit
Report

1 Answer

1

Looks like you solved your own problem.

Edit: Personally, I would actually enumerate the assemblies, one by one, load them and look for the interface. Do it based of files rather than what the AppDomain is doing.

answered 2010-08-23T22:50:32.513

Your Answer