Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created April 30, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/5c71679db96da46fa4b1e876b4cfe4df to your computer and use it in GitHub Desktop.
Save guitarrapc/5c71679db96da46fa4b1e876b4cfe4df to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Hosting;
using Newtonsoft.Json;
using System;
using System.IO;
namespace MyAspNetCoreMvc
{
public class AppVersion
{
public string ShortHash { get; set; } = "ffffff";
public DateTime DeployUtc { get; set; } = DateTime.UtcNow;
}
public interface IAppVersionService
{
/// <summary>
/// Application Version info
/// </summary>
AppVersion AppVersion { get; }
}
/// <summary>
/// Application Version info Service
/// </summary>
public class AppVersionService : IAppVersionService
{
private readonly IHostingEnvironment _hostingEnvironment;
public AppVersionService(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
private AppVersion _appVersion;
public AppVersion AppVersion
{
get
{
if (_appVersion != null)
{
return _appVersion;
}
var path = Path.Combine(_hostingEnvironment.ContentRootPath, "version.json");
if (path == null || !File.Exists(path))
{
_appVersion = new AppVersion();
}
else
{
using (var sr = File.OpenText(path))
{
_appVersion = JsonConvert.DeserializeObject<AppVersion>(sr.ReadToEnd());
}
}
return _appVersion;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment