Using Configuration with Azure Function

Introduction
Sometimes we need want to store configuration data in our Azure Function which we can be referenced in our Azure Function, these configuration data includes storing server urls, connection strings, credentials etc. This is very useful when we want to deploy our Azure function in different environment, we don’t need to do any changes in our Azure function code, we can simply use Configuration to hold new details and our Azure function will be using the new details. Let’s see how we can use configuration in Azure function.

Details
This post assumes you have basic understanding of the Azure function and how to work with it. If you are new to Azure function, you can refer details here. Back to our topic, Let’s say we have requirement to store our application user details and other settings which we need in our function app to connect with Dynamics 365 CE. To store these information we need to use two steps mainly.

1. Update your Azure function to use ConfigurationManager.AppSetting
2. Add all the Azure function settings in Azure Portal

let say we want use application settings for blob container name and for the blog connection details. First thing we need to do is add reference to System.Configuration in our Azure function after that we can sue following code.

var BlobConnection = ConfigurationManager.AppSettings["BlobConnection"];
var Containername =  ConfigurationManager.AppSettings["Containername"];

In above code we are using two Keys BlobConnection and Containername, we need to add configuration in Azure function using same key names. Similarly we can use other keys in other methods, for example below method we are using to get connected with Dynamics 365 CE.

 private static OrganizationWebProxyClient GetCRMService(TraceWriter log)
        {

            var aadInstance = "https://login.microsoftonline.com/";
            var organizationUrl = ConfigurationManager.AppSettings["Dynamics365URL"];

            var clientId = ConfigurationManager.AppSettings["ClientId"];
            var clientkey = ConfigurationManager.AppSettings["Clientkey"];
            var tenantId = ConfigurationManager.AppSettings["TenantId"];


            var clientcred = new ClientCredentials(clientId, clientkey);

            var authenticationContext = new AuthenticationContext(aadInstance + tenantId);

            var authenticationResult = authenticationContext.AcquireTokenAsync(organizationUrl, clientcred);

            var requestedToken = authenticationResult.Result.AccessToken;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var sdkService = new OrganizationWebProxyClient(GetServiceUrl(organizationUrl), new TimeSpan(30, 0, 0), false);
            sdkService.HeaderToken = requestedToken;

            return sdkService;

        }

Now we have code, we can add configuration setting using Azure portal after deploying our Azure function. Use following Steps

1. Open your Function App
2. Under your Function App setting click on the Configuration
az1
3. Click on New application setting
az2
4. Add all the Key- Values one by one
az3
5. Once all the configuration is added, Click on Save button to save your configuration.

Restart your function app and it should refer all the configuration you added.

Leave a Reply