Reputation: 25349
I am trying to add feature flags to .net core 2.2 console app. The feature flags are defined on Azure App Configuration store.
When the feature flags are defined in the appsettings.json file, I faced a problem, but made it to work. The full console project is defined in my SO question here. This is not the problem.
I am trying to follow the tutorial and this is driving me nuts. The problem here is this tutorial is not simple. Its a web app, on the top of it, he used Secret manager tool.
I simply could not make the webapp work as described in the tutorial and so I am trying to first create a simple console app but so far could not succeed with Azure App Config store.
Here is the console app that did not work so far. Any help is deeply appreciated.
The following are packages are added to the console project.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.FeatureFilters;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
namespace ConfigurationConsoleApp
{
class Program
{
static async Task Main(string[] args)
{
var builder = new ConfigurationBuilder();
var azureConnectionString = "<APP_CONFIGURATION_CONNECTION_STRING>";
//builder.AddAzureAppConfiguration(options =>
//{
// options.Connect(azureConnectionString).UseFeatureFlags();
//});
builder.AddAzureAppConfiguration(azureConnectionString);
var config = builder.Build();
Console.WriteLine(config["TestApp:Settings:Message"] ?? "Hello world!");
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var services = new ServiceCollection();
//services.AddSingleton<IConfiguration>(configuration).AddFeatureManagement().AddFeatureFilter<PercentageFilter>().AddFeatureFilter<AccountIdFilter>();
services.AddSingleton<IConfiguration>(config).AddFeatureManagement();//.AddFeatureFilter<PercentageFilter>().AddFeatureFilter<AccountIdFilter>();
const string FeatureName = "Beta";
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
{
var featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
var enabled = await featureManager.IsEnabledAsync(FeatureName);
Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} ");
}
}
}
}
Upvotes: 0
Views: 3146
Reputation: 730
Did you take a look at the example apps in the Microsoft.FeatureManagement GitHub repository?
https://github.com/microsoft/FeatureManagement-Dotnet/tree/master/examples/FeatureFlagDemo https://github.com/microsoft/FeatureManagement-Dotnet/tree/master/examples/ConsoleApp
Your code looks like it should work. It won't work unless a feature flag is created in the Azure App Configuration instance named "Beta".
Upvotes: 4