Using Configuration With Azure Function

Introduction

 
Sometimes we need to store configuration data in our Azure Function which we can reference.  In our Azure Function, this configuration data includes storing server urls, connection strings, credentials etc. This is very useful when we want to deploy our Azure function in different environments. 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 article assumes you have a basic understanding of the Azure function and how to work with it. If you are new to the Azure function, you can refer to the details here. Back to our topic. Let’s say we have a 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 this information we need to use two steps:
  1. Update your Azure function to use ConfigurationManager.AppSetting
  2. Add all the Azure function settings in Azure Portal
Let's  say we want to use application settings for blob container name and for the blog connection details. The first thing we need to do is add reference to System.Configuration in our Azure function, and after that we can use the following code.
  1. var BlobConnection = ConfigurationManager.AppSettings["BlobConnection"];  
  2. var Containername =  ConfigurationManager.AppSettings["Containername"];  
In the above code we are using two Keys, BlobConnection and Containername. We need to add configuration in Azure function using the same key names. Similarly we can use other keys in other methods, for example, in the  below method we are using it to get connected with Dynamics 365 CE.
  1. private static OrganizationWebProxyClient GetCRMService(TraceWriter log)  
  2.        {  
  3.    
  4.            var aadInstance = "https://login.microsoftonline.com/";  
  5.            var organizationUrl = ConfigurationManager.AppSettings["Dynamics365URL"];  
  6.    
  7.            var clientId = ConfigurationManager.AppSettings["ClientId"];  
  8.            var clientkey = ConfigurationManager.AppSettings["Clientkey"];  
  9.            var tenantId = ConfigurationManager.AppSettings["TenantId"];  
  10.    
  11.    
  12.            var clientcred = new ClientCredentials(clientId, clientkey);  
  13.    
  14.            var authenticationContext = new AuthenticationContext(aadInstance + tenantId);  
  15.    
  16.            var authenticationResult = authenticationContext.AcquireTokenAsync(organizationUrl, clientcred);  
  17.    
  18.            var requestedToken = authenticationResult.Result.AccessToken;  
  19.            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;  
  20.            var sdkService = new OrganizationWebProxyClient(GetServiceUrl(organizationUrl), new TimeSpan(30, 0, 0), false);  
  21.            sdkService.HeaderToken = requestedToken;  
  22.    
  23.            return sdkService;  
  24.    
  25.        }  
Now we have the code, we can add configuration settings using Azure portal after deploying our Azure function. Use the following steps,
  1. Open your Function App
  2. Under your Function App setting click on the Configuration



  3. Click on New application setting



  4. Add all the Key- Values one by one



  5. Once all the configuration is added,  click on the Save button to save your configuration.
Restart your function app and it should refer all the configurations you added.


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.