【问题标题】:How to retrieve Product Version?如何检索产品版本?
【发布时间】:2016-03-09 22:07:02
【问题描述】:

有没有办法检索 ASP.NET 5 Web 应用程序的产品版本?

这就是我的project.json

"version": "4.0.0-alpha1"

我怎样才能从应用程序中检索它?我以前可以在旧 ASP.NET 版本上执行此操作:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

但是,现在它一直给我0.0.0.0。有什么想法吗?

【问题讨论】:

标签: c# asp.net-core versioning asp.net-core-mvc


【解决方案1】:

如果您还没有,请在您的 project.json 文件中添加对 System.Reflection 的引用。

"dependencies": {
    "System.Reflection": "4.1.0-beta-23516" // Current version at time of posting
}

然后,您可以从AssemblyInformationalVersionAttribute InformationalVersion 属性中获取值。

private static string GetRuntimeVersion() =>
        typeof(SomeClassInYourAssembly)
            .GetTypeInfo()
            .Assembly
            .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
            .InformationalVersion;

【讨论】:

  • 这不是正确的方法。请考虑我的回答。
  • 这也适用于非 ASP 应用程序,例如。控制台应用程序。但是我更喜欢从Assembly.GetEntryAssembly() 而不是typeof(...).GetTypeInfo().Assembly 获得组装
  • 天哪,经过一周的寻找答案。这适用于 .Net Core 1.1。谢谢!
【解决方案2】:

在任何需要版本的地方注入 IApplicationEnvironment。例如在 Startup 类的 Configure 方法中:

       public void Configure(IApplicationBuilder app, 
        IApplicationEnvironment applicationEnvironment)
    {
        app.UseIISPlatformHandler();

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync(applicationEnvironment.ApplicationVersion);
        });
    }

来源:“启动时可用的服务”http://docs.asp.net/en/latest/fundamentals/startup.html

【讨论】:

  • 你在说什么版本? RC1 给出了这个答案。使用 RC1,我的答案给出了 project.json 中指定的版本。它甚至在 IntelliSense 中这样说:“获取应用程序的版本,如 project.json 文件中指定的那样”。使用 RTM IApplicationEnvironment 甚至不再存在,您必须使用 IHostingEnvironment (docs.asp.net/en/latest/migration/rc1-to-rtm.html)。您的反对票是不必要的。
  • 问题是IHostingEnvironment没有ApplicationVersion成员。
  • 我知道。但同样,这就是 RC1 的答案。如果您想知道如何使用 RTM 进行操作,请提出一个新问题。或者人们是否需要在新版本出现时重新审视他们的答案?请让我知道,然后我确定我会停止回答。
猜你喜欢
  • 2012-09-29
  • 2023-03-16
  • 2015-09-10
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多