Menu Menu
DLL versioning using Git commit ID

DLL versioning using Git commit ID

Whenever we have to debug our live web application, deployed on an IIS web server, it's always helpful to know what version of the source code was used to build that version of the web app, because it allows us to inspect that particular version of the code on our local development environment, if there are any issues to be fixed. One of many ways to accomplish this is to embed the ID of the last git commit, used to build that version of the web app, within the VersionInfo resource of our web app DLL files. An example screenshot below shows how a version info could look like when implemented this way:

Example version dialog of a DLL

When you right-click a dll file in Windows Explorer and switch to the "Details" tab, you can see the VersionInfo data conveniently displayed. One of the fields displayed there is the "Product version", which we can use to store the last git commit id into. The idea is to configure Visual Studio to embed this data, during the build process, by setting up a pre-build event, which would update the Properties/AssemblyInfo.cs file, and set the correct git commit id into the "Product version" field.

So, open your solution in Visual Studio and, in the Solution Explorer, right-click your project and choose Properties:

Example version dialog of a DLL

When a new window displays, switch to the "Build Events" tab, as shown in the following screenshot:

Example version dialog of a DLL

Now, assuming we have git installed and we added a path to the git's "bin" folder to the global PATH environment variable (which should happen automatically, during the installation of git), we should type in the following powershell commands inside the "Pre-build event command line" text box:

powershell -Command "(gc $(ProjectDir)\Properties\AssemblyInfo.cs) -replace 'assembly: AssemblyInformationalVersion\(".*"\)',('assembly: AssemblyInformationalVersion(\"git-{0}\")' -f (git rev-parse --short HEAD)) | Out-String | Out-File -Encoding UTF8 $(ProjectDir)\Properties\AssemblyInfo.cs"

powershell -Command "(gc -Raw $(ProjectDir)\Properties\AssemblyInfo.cs) -replace '(\r\n){3,}','$1' | Out-File -Encoding UTF8 $(ProjectDir)\Properties\AssemblyInfo.cs"

The first PS command replaces the value of the "AssemblyInformationalVersion" field, in the file Properties/AssemblyInfo.cs, with the last git commit id, using the command "git rev-parse --short HEAD". The second PS command handles the excessive new lines at the end of the file, introduced by Power Shell in the first command (if you find a better solution for this, please let me know and I'll update this article).

At this moment, you can try to build your solution and take a look in the bin/ folder to see if your dll files now have the git commit id in their VersionInfo.

interesting read? share it!

About the author

Mladen Bukejlovic
Software Developer

Mladen likes to consider himself as a problem solver, rather than a software developer. He's always looking for an interesting problem to solve, no matter which realm it comes from. He believes that solutions for problems from one realm can usually be adapted and applied to problems from a completely different one, which, as a result, helps to grow the overall experience of a person a lot faster.


Maurício Hello! thank you very much for your posting ... I was just in need of this, to check at runtime if the DLL had been changed to perform a procedure, because one can change the DLL without restarting the WEB site service! Just one detail, which I took to observe is the change of the versioning class: From AssemblyVersion to AssemblyInformationalVersion !!! Thank you and success!

Do you have something on your mind? Feel free to share your thoughts with us.

Marija Lazarevic and Aleksandar Milic at Vega IT
Latest blog posts
Kindness at Work: Does It Actually Work?
Vuk Kockar talking about public speaking 6 Tips How to Overcome the Fear of Public Speaking
STAR conference audience at Vega IT Internal Conferences: The Power of Knowledge and Teamwork
Audience sitting at the internal STAR conference at Vega IT Public Speaking: From the Audience to the Stage
Java for C# developers handbook Java for C# Developers