Created
April 30, 2019 12:18
-
-
Save guitarrapc/5c71679db96da46fa4b1e876b4cfe4df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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