App Configuration Service with API
App Configuration Service with API

ASP .NET Core Web API using Azure App Configuration Service

In previous two articles, we have discussed about how to create an instance of Azure App Configuration Service and we also have discussed about how to add key value pairs in it.

In this article, we are going to demonstrate how to use the configuration values from Azure App Configuration Service from an ASP .NET Core Web API.

Create Azure App Configuration Service Instance

For following all steps in this article, we will need Azure Subscription. If you don’t have an Azure subscription, create a free account before you begin.

You will also need to create a new App Configuration store. You can refer the detailed steps from the previous article. Once the resource is created, you can follow the next steps to create the key-value pair.

Add Configuration Settings

Once the Azure App Configuration Service instance is created, the next step is to add the configuration settings here. We can navigate to Operations -> Configuration explorer  -> Create -> Key-value option to add the key-value pairs, which would be used by the applications. 

Use the same panel to create two key-value pairs. The key and value pairs are provided below. We are assuming that we want to show two different greeting messages, depending on whether GET or POST endpoint was called.

Key = TestApi:GetEndpoint:Message, Value = Hello

Key = TestApi:PostEndpoint:Message, Value= Good Morning

Azure Portal – Add key-value pairs to be used from Web API

Create And Configure ASP .NET Core Web API App

Now, let’s create an ASP .NET Core Web API Project, namely TestApi, targeting to .NET 7 using Visual Studio.

Visual Studio – Option selected while creating an ASP .NET Core Web API Project

Once the, TestApi, project is created, we need to add package reference to the NuGet package Microsoft.Azure.AppConfiguration.AspNetCore using the command mentioned in the code snippet given below.

Then there are two other commands to initialize Secret Manager to store a secret named ConnectionStrings:AppConfig, which stores the connection string for your App Configuration store. As mentioned in the comments of the code snippet, replace the token shown within two double quotes (i.e. {app_config_connection_string}) , with actual connection string.

In Azure Portal, to the resource and select Settings -> Access Keys from the side navigation menu. Select the tab Read-only keys. Then copy the connection string provided in this tab. Use this value to replace the replace the token shown within two double quotes (i.e. {app_config_connection_string}) shown in the code snippet above.

Azure App Configuration Service – Connection String

Now, let’s go to TestApi project and open Program.cs file. In that file let’s add the code to read the connection string and then use that connection string while adding App Configuration Service, as shown in the code snippet given below.

Now, we will need a class where the application settings would be deserialized. SO, let’s create a new folder, Configurations. Under this folder, let’s create a new class AppSettings.cs. The contents of this class is shown in the code snippet given below.

Then, in Program.cs file, we need to add configurations to bind the settings from TestApi: namespace to the AppSettings type

Reading From App Configuration Service

Now, add a new API Controller, GreetingsController.cs under Controllers folder. Add a constructor which takes IOptionSnapshot<AppSettings> as parameter. We need to hold the Value property of this parameter as an instance member.

Then, let’s add two endpoints, one GET endpoint and the other POST endpoint. Each of the endpoint will take a string parameter. Then, we can use AppSettings object to read the respective greetings messages. The greetings will be prefixed to the input parameter and then the result is returned.

The complete code of the controller is shown in the code snippet given below.

Run and Verify

Now, if we run the API project, we will be able to call the APIs from Swagger UI. Let’s call get endpoint and we can see that the endpoint prefixes appropriate message to the passed input value.

ASP .NET Core API – is now connected to Azure App Configuration Service

Wrapping Up

We have used Secrets Manager in this demo. It helps to keep the secrets outside the repository. When the API project is deployed, we can read the configurations from Connection stringsApplication settings or environment variables.

Alternatively, to avoid connection strings all together, you can connect to App Configuration using managed identities or your other Azure AD identities.

I hope you find this information helpful. Let me know your thoughts.

Leave a Reply Cancel reply