Feature Management using Consul KV store in ASP.Net core
Tags: dotnet, consul, webdev, programming
Published: 2022-01-07 17:04:24


A Practical guide on how Consul KV is used for managing feature flags in ASP.NET core application

![Image by [Nitesh Singhal](https://medium.com/@niteshsinghal85)](https://cdn-images-1.medium.com/max/2506/1*xJ4o7_b9lXJd3Dxeh7sudQ.png)

**Feature Management or Flags** is a concept to switch features on and off via a flag. You want to switch on some feature based on certain condition or at certain time. but at the same time you also don’t want to recompile and redeploy your app.

You can find more information on why feature flags are required [here](https://docs.microsoft.com/en-us/azure/azure-app-configuration/concept-feature-management?WT.mc_id=dotnet-11820-chnoring).

This concept looks interesting and could be helpful in many scenarios. So I decide to give it a try and started following given article.

[**Quickstart for adding feature flags to ASP.NET Core**](https://docs.microsoft.com/en-us/azure/azure-app-configuration/quickstart-feature-flag-aspnet-core?tabs=core5x)

Everything was going good until I realize that I don’t have azure account( I know I can create a free account but just for argument sake, I don’t). so I had look for alternatives. I found few alternatives and most of them needed to use any external service.

I did not wanted to use external service and was looking for something locally manageable.

So first I configured the feature flags in appsettings.json file

![feature management in appsettings.json](https://cdn-images-1.medium.com/max/2000/1*2CUSYyVC2Ywb9Davmq5LlQ.png)

This did the trick and was able to manage the features locally.

but this also has drawbacks that I have to update the appsettings.json file for each application if this feature is spanning across multiple microservices.

then I was looking for some way of managing this configuration centrally so that I don’t have to go to each app and update the appsettings.json

That’s where **consul KV store** came into picture. I learned that it can be used for managing the configuration centrally so I decided to give it a try.

![](https://cdn-images-1.medium.com/max/2000/1*ELlcZRDoJQd2AC1TacDBgQ.png)

### Setting up Consul KV store

First let’s setup the consul locally. I am using docker version but it can also be install without docker.

    docker run -d -p 8500:8500 --name=demo-consul consul

let’s verify if it running. Go to [http://localhost:8500](http://localhost:8500)

![consul ui](https://cdn-images-1.medium.com/max/2512/1*Y4oFE0IWr8D2jpWPp-tPeg.png)

as we can see it is running successfully. Now let’s add configuration in Key/Value store.

Click the ‘**Key/Value’** from side menu and click ‘**Create**’

![Consul UI KV store](https://cdn-images-1.medium.com/max/2530/1*zac7yL2gUy7LK94F-O0tYg.png)

Add Key “**FeatureManagement**”. *(it can be anything of your choice but remember to use same in code)*

and add the same content as we have added in the appsettings.json

![Adding new key value consul UI](https://cdn-images-1.medium.com/max/2540/1*SfFnhvuy6lGrtBR_xaveGQ.png)

Save it in the end.

We are done with consul set up. Let’s move onto code and see what changes are needed.

![](https://cdn-images-1.medium.com/max/2000/1*ELlcZRDoJQd2AC1TacDBgQ.png)

### Demo example

First of all we will need a way to read the KV data and add it as configuration

I am using nuget package to fetch the data from consul KV store and add it to configuration.

    dotnet add package Winton.Extensions.Configuration.Consul

Open Program.cs and add the following code.
{% gist https://gist.github.com/niteshsinghal85/f8aab76262638e17f74c19a52ff86c6e.js %}
add the following line in appsettings.json for configuring consul address

    "ConsulServer": "http://localhost:8500"

with these changes we are ready to run our app.

![App with feature is disabled](https://cdn-images-1.medium.com/max/2000/1*lA1Tb3SnjaJ8dpivl4bzug.png)

Since our feature flag “Beta” was set to false we don’t see that menu item yet.

now let’s change the value in consul KV and set it true.

![](https://cdn-images-1.medium.com/max/2000/1*X3bTH1gM9IiipbbDGaS8rg.png)

save it and refresh the browser by hitting F5.

![App with feature is enabled](https://cdn-images-1.medium.com/max/2000/1*GjfstQrFfXLcKEEdWKw5fQ.png)

Now you can see “Beta” in the menu and when you click it will take you to the respective page.

![feature page](https://cdn-images-1.medium.com/max/2000/1*oVJluihm9pk5qctqHaF7lg.png)

with this technique we can now manage our configuration from a central location and if there are multiple microservice which needs same configuration can be updated at the same time.

![](https://cdn-images-1.medium.com/max/2000/1*ELlcZRDoJQd2AC1TacDBgQ.png)

### Restrict Consul KV access

Till now we have seen that we have not restricted the consul access and anybody can access and modify the values. this is ok for development but we don’t want that in production so we have to restrict the access.

we can follow the given guide and secure consul access.
[**Secure Consul with Access Control Lists (ACLs) | Consul - HashiCorp Learn**](https://learn.hashicorp.com/tutorials/consul/access-control-setup-production)

![](https://cdn-images-1.medium.com/max/2000/1*ELlcZRDoJQd2AC1TacDBgQ.png)

## Summary

In this tutorial, I covered feature flags and consul KV store usage in combination. but the usage of consul KV store is not limited to configuration storage. we can also use this as caching tool for some of scenarios.

Hope it is helpful..

*Happy coding and Keep learning..!*

Source code at [Github](https://github.com/niteshsinghal85/blogs/tree/main/TestFeatureFlags)
[**Consul by HashiCorp**](https://www.consul.io/)

Originally published on [Medium](https://medium.com/@niteshsinghal85/feature-management-using-consul-kv-store-in-asp-net-core-14f597251cca)