Reputation: 2709
Is there a way to retrieve the product version of an ASP.NET 5 web application?
This is what I have in my project.json
:
"version": "4.0.0-alpha1"
How would I be able to retrieve this from within the application? I used to be able to do this on older ASP.NET versions:
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
However, now it just gives me 0.0.0.0
all the time. Any ideas?
Upvotes: 2
Views: 2456
Reputation: 7128
For a command line application with .NET 6, I found this to work.
(Anything using the Assembly Location seems to fail when the application is published to a self-contained file.)
First, set the version (in this example: 1.4.3) in the properties: (search for "version")
Then use this code to get the number you put in:
using System.Linq;
using System.Reflection;
var version = typeof(Version) // "Version" is the name of any local class
.GetTypeInfo()
.Assembly
.GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.First()
.InformationalVersion;
Upvotes: 0
Reputation: 5366
Inject IApplicationEnvironment anywhere you need the version. So for instance in the Configure method of the Startup class:
public void Configure(IApplicationBuilder app,
IApplicationEnvironment applicationEnvironment)
{
app.UseIISPlatformHandler();
app.Run(async (context) =>
{
await context.Response.WriteAsync(applicationEnvironment.ApplicationVersion);
});
}
Source: "Services Available in Startup" http://docs.asp.net/en/latest/fundamentals/startup.html
Upvotes: 2
Reputation: 10879
Add a reference to System.Reflection
in your project.json file if you don't already have one.
"dependencies": {
"System.Reflection": "4.1.0-beta-23516" // Current version at time of posting
}
Then, you can get the value from the AssemblyInformationalVersionAttribute InformationalVersion
property.
private static string GetRuntimeVersion() =>
typeof(SomeClassInYourAssembly)
.GetTypeInfo()
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
Upvotes: 7