I have struggled with this for a bit, and I have a feeling I am very close. In a .NET MVC web application I have used to have assemblyinfo information displayed in the front end without issue. In a bit of optimization I wanted to move that code out to a general purpose helper class. For ease of use I have made it a static class, but I have hit several snags in the process. But now it throws a System.StackOverflowException when I try to use it, sadly. Here is the code:
public static class VersionInformationHelper
{
public static string GetVersionNumber
{
get
{
if (!string.IsNullOrWhiteSpace(GetVersionNumber.GetType().Assembly.GetName().Version.ToString()))
{
return "v" + GetVersionNumber.GetType().Assembly.GetName().Version.ToString();
}
else
{
return string.Empty;
}
}
}
/// <remark>
/// This doesnt exactly return the commit hash so to speak. Well it does, but Teamcity is set to enter the corresponding commit hash information when building,
/// into productversion in "AssemblyInfo.cs". It could be any string really. But we assume that a commit hash will always be in that location.
/// It's "Assembly informational version" in the assemblyinfo patcher build feature in teamcity.
/// </remark>
public static string GetCommitHash
{
get
{
if (!string.IsNullOrWhiteSpace(System.Diagnostics.FileVersionInfo.GetVersionInfo(GetVersionNumber.GetType().Assembly.Location).ProductVersion))
{
return System.Diagnostics.FileVersionInfo.GetVersionInfo(GetVersionNumber.GetType().Assembly.Location).ProductVersion;
}
else
{
return string.Empty;
}
}
}
public static string GetBuildDate
{
get
{
return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:dd/MM/yy HH:mm:ss}", System.IO.File.GetLastWriteTime(GetVersionNumber.GetType().Assembly.Location));
}
}
}
EDIT
Fixed code based on feedback (GetVersionNumber and GetCommitHash" has been changed):
public static class VersionInformationHelper
{
public static string GetVersionNumber
{
get
{
if (!string.IsNullOrWhiteSpace(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()))
{
return "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
else
{
return string.Empty;
}
}
}
/// <remark>
/// This doesnt exactly return the commit hash so to speak. Well it does, but Teamcity is set to enter the corresponding commit hash information when building,
/// into productversion in "AssemblyInfo.cs". It could be any string really. But we assume that a commit hash will always be in that location.
/// It's "Assembly informational version" in the assemblyinfo patcher build feature in teamcity.
/// </remark>
public static string GetCommitHash
{
get
{
if (!string.IsNullOrWhiteSpace(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).ProductVersion))
{
return System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).ProductVersion;
}
else
{
return string.Empty;
}
}
}
public static string GetBuildDate
{
get
{
return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:dd/MM/yy HH:mm:ss}", System.IO.File.GetLastWriteTime(GetVersionNumber.GetType().Assembly.Location));
}
}
}