从静态类获取程序集版本信息会引发堆栈溢出异常

时间:2017-06-29 13:26:03

标签: c# asp.net-mvc

我有点挣扎,我觉得我非常接近。在.NET MVC Web应用程序中,我曾经常常在前端显示assemblyinfo信息而没有问题。在一些优化中,我想将该代码移到一个通用的助手类。 为了便于使用,我把它变成了一个静态类,但是我在这个过程中遇到了几个障碍。但是现在它在我尝试使用它时会抛出一个System.StackOverflowException,可悲的是。这是代码:

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));
        }
    }
}

修改

基于反馈的固定代码(GetVersionNumber和GetCommitHash“已更改):

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));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您正在从同一GetVersionNumber(两次)的getter中读取GetVersionNumber。这将无限循环(或直到发生堆栈溢出)。

您可能需要使用Assembly.GetExecutingAssembly().GetName().Version.ToString()或其他方法更改两次出现以获取版本。