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:
- Update your Azure function to use ConfigurationManager.AppSetting
- 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.
- var BlobConnection = ConfigurationManager.AppSettings["BlobConnection"];
- 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.
- 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 the code, we can add configuration settings using Azure portal after deploying our Azure function. Use the following steps,
- Open your Function App
- Under your Function App setting click on the Configuration
- Click on New application setting
- Add all the Key- Values one by one
- 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.