Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting assembly version info from static class throws a stackoverflow exception

Tags:

c#

asp.net-mvc

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));
        }
    }
}
like image 762
Frederik T Avatar asked Jul 29 '26 13:07

Frederik T


1 Answers

You are reading GetVersionNumber from the getter of same GetVersionNumber (twice). This will loop infinitely (or until a stack overflow occurs).

You probably need to change the two occurences with Assembly.GetExecutingAssembly().GetName().Version.ToString() or another method to get the version.

like image 158
Spikolynn Avatar answered Aug 01 '26 01:08

Spikolynn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!