views:

3828

answers:

7

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() but it just gives me the dynamically compiled assembly (returning a version number of 0.0.0.0).

+6  A: 

For web applications i have always used the Web.Config to store the current version of the site and another setting to show/hide it in the site footer for version control on staging and production.

You can also try the following:

create AssemblyInfo.cs file in the web application root that has the following

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

then use

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() 

Here is an example of what a AssemblyInfo.cs should look like

Ioxp
Retrieving the version from the actual web application is fairly easy, but can only be done in code behind - using System.Reflection.GetExecutingAssembly().GetName().Version.ToString().I would think there would be some way of retrieving it from a referenced assembly though...
Duffman
Did you try the above?
Ioxp
+1  A: 

Some info here: http://www.velocityreviews.com/forums/showpost.php?p=487050&postcount=8

in asp.net 2.0 each page is built into it own assembly, so only the dll the AssemblyInfo.cs is built into will return the correct answer. just add a static method to AssemblyInfo.cs that returns the version info, and call this method from your other pages.

-- bruce (sqlwork.com)

But I wrote a simple method to do that:

    public static string GetSystemVersion(HttpServerUtility server)
    {
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.Load(server.MapPath("~/web.config"));
        System.Xml.XmlNamespaceManager ns = new System.Xml.XmlNamespaceManager(doc.NameTable);
        ns.AddNamespace("bla", "http://schemas.microsoft.com/.NetConfiguration/v2.0");

        System.Xml.XmlNode node = doc.SelectSingleNode("/bla:configuration/bla:system.web/bla:authentication/bla:forms[@name]", ns);

        string projectName = "";
        if (node != null && node.Attributes != null && node.Attributes.GetNamedItem("name") != null)
            projectName = node.Attributes.GetNamedItem("name").Value; //in my case, that value is identical to the project name (projetname.dll)
        else
            return "";

        Assembly assembly = Assembly.Load(projectName);
        return assembly.GetName().Version.ToString();
    }
Seiti
A: 

If you are looking for this from a web control, one hack is to find the type of the code-behind Page (ie. the class that inherits from System.Web.UI.Page). This is normally in the consumer's web assembly.

Type current, last;
current = Page.GetType();
do
{
    last = current;
    current = current.BaseType;
} while (current != null && current != typeof(System.Web.UI.Page));
return last;

I hope there is a better way.

Matt Woodard
+3  A: 

To add to the responders that have already posted. In order to get the assembly version in an ASP.Net web application you need to place a method in the code behind file similar to:

protected string GetApplicationVersion() {
    return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}

In the ASPX page you want to display the version number simply place:

<%= GetApplicationVersion() %>
Amir Khawaja
A: 
Version version = new Version(Application.ProductVersion);
string message = version.ToString();
noname
A: 

I encountered a similar problem, and thought you might find the solution useful.

I needed to report the current application version (of a web application project) from a custom server control, where the server control was contained in a different library. The problem was that the "easiest" assembly getters did not provide the right assembly.

  • Assembly.GetExecutingAssembly() returned the assembly containing the control; not the application assembly.
  • Assembly.GetCallingAssembly() returned different assemblies depending on where I was at in the call tree; usually System.Web, and sometimes the assembly containing the control.
  • Assembly.GetEntryAssembly() returned null.
  • new StackTrace().GetFrames()[idx].GetMethod().DeclaringType.Assembly retrieves the assembly of a frame in the stack trace at index idx; however, besides being inelegant, expensive, and prone to miscalculation on the frame index, it is possible for the stack trace to not contain any calls to the application assembly.
  • Assembly.GetAssembly(Page.GetType()) scored me the App_Web_@#$@#$%@ assembly containing the dynamically generated page. Of course, the dynamic page inherits a class from my application assembly, so that led to the final solution:

Assembly.GetAssembly(Page.GetType().BaseType)

With the assembly reference in hand, you can drill to the version through its name:

var version = Assembly.GetAssembly(Page.GetType().BaseType)
                      .GetName()
                      .Version;

Now, this solution works because I had a reference to a type from the application assembly. We don't use any pages that do not inherit from a code behind, so it happens to be effective for us, but your mileage may vary if your organization's coding practices are different.

Happy coding!

kbrimington