Get web application assembly name, regardless of current executing assembly

15,773

Solution 1

Try

BuildManager.GetGlobalAsaxType().BaseType.Assembly

Solution 2

You can use

HttpContext.Current.ApplicationInstance.GetType().Assembly

Solution 3

I know this is an old question but this was my approach to a somewhat similar situation. In my case a was using another assembly for formatting a string with the version to show for multiple programs that have the same core.

Version v = null;
var a = Assembly.GetEntryAssembly() ?? GetWebEntryAssembly() ?? Assembly.GetExecutingAssembly();
SnapshotVersion = FileVersionInfo.GetVersionInfo(a.Location).ProductVersion;
if (ApplicationDeployment.IsNetworkDeployed)
{
    var d = ApplicationDeployment.CurrentDeployment;
    v = d.CurrentVersion;
    v = new Version(v.Major, v.Minor, v.Revision);
}
else
    v = a.GetName().Version;
if (v != null)
    version = string.Format("{0}.{1}.{2}", v.Major, v.Minor, v.Build);

Because this is in a static constructor all I needed to do was to call any property of this static class from the Web Application and then find the last calling assembly that is different from the assembly that the static class is on. This was achieve with the method GetWebEntryAssembly.

private static Assembly GetWebEntryAssembly()
{ 
    var frames = new StackTrace().GetFrames();
    var i = frames.FirstOrDefault(c => Assembly.GetAssembly(c.GetMethod().DeclaringType).FullName != Assembly.GetExecutingAssembly().FullName).GetMethod().DeclaringType;
    return Assembly.GetAssembly(i);
}
Share:
15,773
Pablote
Author by

Pablote

Updated on July 18, 2022

Comments

  • Pablote
    Pablote almost 2 years

    Is is possible to get the assembly name of an ASP.NET web application, from a referenced assembly??

    Assembly.GetEntryAssembly worked fine in desktop and console apps but it seems to be always null in web apps, and GetExecuting\GetCallingAssebly return my referenced assembly, not the one from the web app.


    Long explanation:

    I wrote a custom Settings Provider, that instead of reading configuration from the app config file, it gets the settings from a centralized configuration service.

    The custom provider is in a separate assembly so it can be used by the different applications.

    The ApplicationName property needs to be overriden with the app assembly name.

    The way to use the provider is though a .net custom attribute, so I can't send any params to it.

    Since non of the Assembly.Get*Assembly methods seem to work, the only thing a I can think of is requiring an appSetting with the app name for web apps, but I'm not really happy with that. Any help with this is appreciated, thanks!

  • Pablote
    Pablote over 12 years
    it's not working :/ ... ApplicationInstance is null in the Application_Start() method (where the settings provider is initialized), and inside the controllers returns a weird runtime generated assembly (e.g. App_global.asax.hiqe17zm, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)
  • SLaks
    SLaks over 12 years
    @Pabote: Try getting the assembly of its BaseType (which should be defined in user code)
  • Tomas
    Tomas almost 12 years
    I get "This method cannot be called during the application's pre-start initialization stage." error while trying to use this method in ASP.NET MVC.
  • Slavo
    Slavo over 10 years
    This returns "mscorlib" for me.
  • Slavo
    Slavo over 10 years
    This doesn't work if you don't include a Global.asax file in your project.
  • Slavo
    Slavo over 10 years
    This doesn't work if you don't include a Global.asax file in your project.