Get web application assembly name, regardless of current executing assembly

前端 未结 3 1177
傲寒
傲寒 2020-12-17 14:52

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

相关标签:
3条回答
  • 2020-12-17 15:17

    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);
    }
    
    0 讨论(0)
  • 2020-12-17 15:28

    Try

    BuildManager.GetGlobalAsaxType().BaseType.Assembly
    
    0 讨论(0)
  • 2020-12-17 15:29

    You can use

    HttpContext.Current.ApplicationInstance.GetType().Assembly
    
    0 讨论(0)
提交回复
热议问题