Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Azure Functions Version

RSS
Modified on Wed, Feb 05, 2020, 7:39 AM by Administrator Categorized as Azure, ·Net Framework

Overview

This article provides reusable code for building a Version API for an Azure Function App.

Reusable Code

The following code, when included in an Azure Function App, provides a "GET api/Version" API which returns the following.

  • Deployment Timestamp
  • Version Number, as specified in a "Version.txt" file.

NOTE: The "Version.txt" file must have its Build Action property set to "Embedded resource".

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.