3

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));
        }
    }
}
2
  • 1
    side note: methods in property form are a bad idea as generally speaking they don't get optimised; best to keep properties as properties and if it's a method it should be a method Commented Jun 29, 2017 at 13:39
  • 1
    Good information, I will keep that in mind :) Commented Jun 29, 2017 at 13:41

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply, following your tips I made some modifications and it seems to be working :) I have updated my original post with the fixed code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.