Using the Web Application version number from an assembly (ASP.NET/C#)

后端 未结 12 975
我寻月下人不归
我寻月下人不归 2020-12-07 18:39

How do I obtain the version number of the calling web application in a referenced assembly?

I\'ve tried using System.Reflection.Assembly.GetCallingAssembly().GetName

相关标签:
12条回答
  • 2020-12-07 19:14

    I prefer the Web.Config to store the current version of the site.

    You can also try create an AssemblyInfo.cs file in the web application root that has the following:

    using System.Reflection;
    using System.Runtime.CompilerServices;
    ...
    [assembly: AssemblyVersion("1.0.*")]
    ...
    

    then access the value via the code like this:

    System.Reflection.Assembly.GetExecutingAssembly()
    

    Here is more informaiton on the AssemblyInfo class.

    0 讨论(0)
  • 2020-12-07 19:14

    HttpContext.Current.ApplicationInstance is derived from the class in the global.asax.cs. You can do the following

     var instance = HttpContext.Current.ApplicationInstance;
     Assembly asm = instance.GetType().BaseType.Assembly;
     System.Version asmVersion = asm.GetName().Version;
    

    It works both in ASP.NET (ASPX) and ASP.NET MVC

    0 讨论(0)
  • 2020-12-07 19:14

    The question states with no reference (instances) it did not (originally) say with no knowledge of web application types.

    EDIT the OP clarified to state that yes they do really require no knowledge of types within the calling web assembly, so the answer is appropriate. However I would seriously consider refactoring such a solution such that the version is passed into the other assembly.

    For most people in this scenario if you know the custom HttpApplication type:

     typeof(MyHttpApplication).Assembly.GetName().Version
    

    and if you only have a dynamic generated type:

     typeof(DynamiclyGeneratedTypeFromWebApp).BaseType.Assembly.GetName().Version
    

    Stop voting me down for this answer :)

    0 讨论(0)
  • 2020-12-07 19:19

    Just in case anyone is still interested; this should do the trick and should be a tad safer than just taking the BaseType of ApplicationInstance to get your hands on the Global.asax implementation.

    Global.asax is always compiled into the same assembly as the assembly attributes from AssemblyInfo.cs, so this should work for all web applications that define a Global.asax.

    For those that don't define their own Global.asax, it will fall back to the version of the generated global_asax type, which is always 0.0.0.0, and for applications that aren't web applications, it will just return no version at all.

    Bonus; using the BuildManager class does not require an active HttpContext instance, which means you should be able to use this from application startup code as well.

    public static Version GetHttpApplicationVersion() {
      Type lBase = typeof(HttpApplication);
      Type lType = BuildManager.GetGlobalAsaxType();
    
      if (lBase.IsAssignableFrom(lType))
      {
        while (lType.BaseType != lBase) { lType = lType.BaseType; }
        return lType.Assembly.GetName().Version;
      }
      else
      {
        return null;
      }
    }
    
    0 讨论(0)
  • 2020-12-07 19:21
    Version version = new Version(Application.ProductVersion);
    string message = version.ToString();
    
    0 讨论(0)
  • 2020-12-07 19:25

    Here is some code I use that supports getting the application's "main" assembly from either Web or non-web apps, you can then use GetName().Version to get the version.

    It first tries GetEntryAssembly() for non-web apps. This returns null under ASP.NET. It then looks at HttpContext.Current to determine if this is a web application. It then uses the Type of the current HttpHandler - but this type's assembly might be a generated ASP.NET assembly if the call is made from with an ASPX page, so it traverses the HttpHandler's BaseType chain until it finds a type that isn't in the namespace that ASP.NET uses for its generated types ("ASP"). This will usually be a type in your main assembly (eg. The Page in your code-behind file). We can then use the Assembly of that Type. If all else fails then fall back to GetExecutingAssembly().

    There are still potential problems with this approach but it works in our applications.

        private const string AspNetNamespace = "ASP";
    
        private static Assembly getApplicationAssembly()
        {
            // Try the EntryAssembly, this doesn't work for ASP.NET classic pipeline (untested on integrated)
            Assembly ass = Assembly.GetEntryAssembly();
    
            // Look for web application assembly
            HttpContext ctx = HttpContext.Current;
            if (ctx != null)
                ass = getWebApplicationAssembly(ctx);
    
            // Fallback to executing assembly
            return ass ?? (Assembly.GetExecutingAssembly());
        }
    
        private static Assembly getWebApplicationAssembly(HttpContext context)
        {
            Guard.AgainstNullArgument(context);
    
            object app = context.ApplicationInstance;
            if (app == null) return null;
    
            Type type = app.GetType();
            while (type != null && type != typeof(object) && type.Namespace == AspNetNamespace)
                type = type.BaseType;
    
            return type.Assembly;
        }
    

    UPDATE: I've rolled this code up into a small project on GitHub and NuGet.

    0 讨论(0)
提交回复
热议问题