GetEntryAssembly for web applications

This seems to be a reliable, simple way to get the “entry” or main assembly for a web app.

If you put controllers in a separate project, you may find that the base class of ApplicationInstance is not in the same assembly as your MVC project that contains the Views – but, this setup seems pretty rare (I mention it because I’ve tried this setup at one point, and a while back a few blogs supported the idea).

    static private Assembly GetWebEntryAssembly()
    {
        if (System.Web.HttpContext.Current == null ||
            System.Web.HttpContext.Current.ApplicationInstance == null) 
        {
            return null;
        }

        var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
        while (type != null && type.Namespace == "ASP") {
            type = type.BaseType;
        }

        return type == null ? null : type.Assembly;
    }

Leave a Comment