AssemblyVersion and Web Projects

Let’s say you want to add an AssemblyVersionAttribute to a web project. The usual practice is to add a file to the project with the name AssemblyInfo.cs. The AssemblyInfo.cs file will live in the App_Code folder, because that is the only location the new web project model will allow stand-alone code files. The contents might look like:

using System.Reflection;

[assembly:
AssemblyVersion("2.1.1.2")]

Next, a web form to display the version number:

<%@ Page Language="C#" %>
<%
@ Import Namespace="System.Reflection" %>

<script runat="server">
  
  
protected void Page_Load(object sender, EventArgs e)
  {            
    
Assembly assembly = Assembly.GetExecutingAssembly();          
    
string version = assembly.GetName().Version.ToString();    
          
    versionLabel.Text = version;
  }
  
</script>

Version:
<asp:Label runat="server" ID="versionLabel" />

The above web form will happily display the version number as 0.0.0.0 because App_Code compiles into a different assembly than the web form. The App_Code assembly will have the version number we need. The ASP.NET compiler doesn’t assign a version number to the dynamically generated assemblies for web forms. This is true in 1.1, too, it’s just that we usually asked for the version number from a code-behind file, and all the code-behind files compiled together into a single assembly.

There are a couple approaches to getting the version number from the AssemblyVersionAttribute in App_Code. One approach would be to substitute the following in our earlier code:

Assembly assembly = Assembly.Load("App_Code");
string version = assembly.GetName().Version.ToString();  

I think an even better approach is to use the Web Deployment Project, which can merge all assemblies into a single assembly, and copy assembly level attributes over, too. If you are using the newer Web Application Project model, then any code in a code-behind file will be able to see the version in an AssemblyVersion source file.

Print | posted @ Wednesday, January 25, 2006 4:28 AM

Comments on this entry:

Gravatar # re: AssemblyVersion and Web Projects
by Gregor Suttie at 1/25/2006 7:43 PM

Thanks for yout reply to this - works a treat.

Cheers
Gregor
  
Gravatar # re: AssemblyVersion and Web Projects
by SteveC at 2/2/2006 8:11 PM

That is great info, thanks for making the post.

Now if I can just get my head around automating the build + deployment via script....

  
Gravatar # re: AssemblyVersion and Web Projects
by John Adams at 4/20/2006 3:52 PM

Not sure about the article is about as it has no intro. Other articles such as...
http://www.odetocode.com/Articles/450.aspx have excellent intros so you have an idea what the article is about. I don't want to be picky, as I am sure the information is great, but most people would be so lost coming to this page.

John
  
Gravatar # re: AssemblyVersion and Web Projects
by scott at 4/20/2006 3:56 PM

John: true. I consider the blog posts to be a little more informal. I don't spend nearly as much time making a proper 'article' out of these posts, which is both good and bad.
  
Gravatar # re: AssemblyVersion and Web Projects
by AKumar at 7/10/2006 4:35 PM

Wondering how one can obtain compile time stamp so that besides the version #, the compile date is obtainable

Kumar
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 2 and 5 and type the answer here: