Auto-Incrementing Assembly Version Number via AssemblyInfo.cs

So I figured I’d connect to my online TFS where my deployments were working (My Trial to Azure has unfortunately ended, however.) and actually try out the auto-increment tactic from the previous post. I booted up my VS2012 and chose to connect to a TFS, I’m going to attempt to use my freshly made TFS Server to make it happen. I haven’t made any configuration changes to it since it’s install!

Image

greenphantom is, as explained in a previous post, my personal PC that I built to work and play on.

Quick rundown on my actual PC specs

  • Operating System: Windows 8.1 64-bit
  • Mother Board: ASUS P8Z77-V
  • RAM: 16GB
  • CPU: Intel i5-3470
  • GFX: AMD Radeon HD 6900
  • Storage: 128GB SSD, 1.5TB external

This will be all sorts of interesting, prepare thyself. I’ve read through the first few chapters of the TFS2012 book I have which covers deployment strategies and installation of TFS, but reading it and thinking ‘wow, that’s easy!’ and actually setting it up are always different.

So I thought it would be interesting to just create the Team Projects through TFS Server Administration Console. When I looked though, I saw this:

Image

Note the ‘?’ How to Add a Team Project, which opens up a web page on the MSDN site on doing it through Visual Studio. Does this mean a Team Project cannot be created from the TFS Admin Console? I guess I find that strange since Visual Studio Online forces you to create the Team Project via the web console.

So I followed directions and created it from Visual Studio 2012

Image

Image

After Clicking Finish, it went through it’s configurations and popped up a message saying the Team Project had been successfully created. I was also able to verify that shortly after it appeared on my TFS admin console under the Default Collections as well as my TFS web access.

Next I created a MVC 4 Web App like I did in my Continuous Deployment blog and that brought me up to a functional web page with any pre-requisite files already included.

UPDATE: The MVC 4 Web App I created used the Razor view engine. The Razor view engine states it enables you to use static HTML code and then add server-side code to it, in order to make the page dynamic. View engines are apparently, however, hot-swappable, so that you may choose the one that works best for you. I haven’t tried changing this out yet… Will have to look into.
(SOURCE: http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx)

I found the AssemblyInfo.cs file in the Solution Explorer under the Properties folder and edited so it had [assembly: AssemblyVersion(“1.0.*”)]

Image

I checked in this change and followed up with a build to see what happens  —-

Well. Nothing visibly. Right-clicking on my Project in the Solution Explorer and choosing properties to view the assembly information showed what I had updated the assemblyinfo.cs to, 1.0.*

In order to see what it had changed to, I need to have the version number visible on my website… except I have no idea how to do that.

So I did some research.

A lot of posts on Stackoverflow show people attempting to use

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

in a header/footer file, it also works in my default.cshtml file, but ‘it works’ in the sense that it doesn’t work. The code above will show 0.0.0.0

The supposed resolution to this is using

@typeof(YourAppNamespace.MvcApplication).Assembly.GetName().Version

Which after finding out exactly what my ‘Namespace’ was, worked!

A namespace is used to organize classes in C#, my html files are .cshtml files.

System.Console.WriteLine("Hello World");

SYSTEM is the namespace, CONSOLE is the class in the namespace

However, namespaces are defined at the beginning of the C# file so they don’t have to be defined every time

using System;
Console.WriteLine("Hello");
Console.WriteLine("World!");

In my situation, my ‘namespace’ is the name of my web application, or quite literally ‘WebApp’. I found this out because as I was typing in the code one of the drop-down suggestions from VS shows {} WebApp and said class next to it. Sweet! and then after I hit ‘.’ it showed the next part – MvcApplication.

@typeof(WebApp.MvcApplication).Assembly.GetName().Version

TFS_VersionNumber_cshtml

Here’s the result after I launched my site up:

Image

Annddd, after doing another build… Voila!

Image

So that works. Technically there is an auto-incrementing version number as we wanted to achieve. The next step is to turn that into a more manageable number so it doesn’t appear so obtrusive.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s