public static class Version { [FunctionName("Version")] public static async Task<HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req, TraceWriter log ) { try { /*--- Get Deployment Date ---*/ var assm = Assembly.GetExecutingAssembly(); var path = assm.Location; DateTime? dt = File.Exists(path) ? File.GetLastWriteTimeUtc(path) : (DateTime?)null; /*--- Get Version ---*/ var version = string.Empty; var resourceName = "ACME.App.Namespace.Version.txt"; using (var stream = assm.GetManifestResourceStream(resourceName)) { using (var reader = new StreamReader(stream)) { version = reader.ReadToEnd(); } } /*--- Prep Content ---*/ var content = new SystemInfo { Version = version, DeploymentDate = dt }; /*--- Return Result ---*/ return new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent(JsonEngine.Serialize(content)) }; } catch (Exception ex) { return new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message) }; } } } [DataContract] internal class SystemInfo { [DataMember] public string DeployedOn { get { return DeploymentDate?.ToString("yyyy-MM-ddTHH:mm:ss.ffffffZ"); } set { } } public DateTime? DeploymentDate { get; set; } [DataMember] public string Version { get; set; } } public static class JsonEngine { public static string Serialize<T>(T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, obj); string retVal = Encoding.UTF8.GetString(ms.ToArray()); return retVal; } public static T Deserialize<T>(string json) { T obj = Activator.CreateInstance<T>(); MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); obj = (T)serializer.ReadObject(ms); ms.Close(); return obj; } }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.