0

First, I do not believe this is a dupe of these:

GetEntryAssembly for web applications

Get entry assembly from ASP.NET application

I have an ASP.NET WebForms app that references a class library. When I call Assembly.GetEntryAssembly() from within the class library while it's referenced by the ASP.NET app, it returns null. However, when I call that same code while the class library is referenced by a console app, it returns the console app assembly.

This answer perhaps sheds light on why Assembly.GetEntryAssembly() behaves differently with ASP.NET:

Why do ASP.NET resolve assembly references differently?

Still, I'd like to be able to get the ASP.NET assembly as if it were a Windows application (console, WPF, WinForms, etc). Is there any way to allow my class library to know about the ASP.NET assembly without adding any ASP.NET references?

Edit: While not the solution I am looking for, this works for the time being. Basically, I use the code below provided by the answerer from one of the questions above to get the ASP.NET assembly from within the web app and then pass it as an argument to a method in my class library.

There's also AppDomain.CurrentDomain.GetAssemblies() which does expose the ASP.NET assembly when called within the class library, but there are like 50 assemblies loaded in the app domain which means you'd need a magic string to get the assembly name. Definitely not an option, but it could work.

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;
}
Community
  • 1
  • 1
oscilatingcretin
  • 9,495
  • 31
  • 111
  • 188

1 Answers1

0

An ASP.NET application is started by IIS worker process, which is a native module, in most cases. Thus, that API should return null to reflect the differences from a console application.

Whatever you want to achieve with that API, find an alternative way instead.

Lex Li
  • 52,595
  • 8
  • 102
  • 129