Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.