I’ve been reading this article: https://docs.microsoft.com/en-us/azure/azure-app-configuration/use-feature-flags-dotnet-core?tabs=core5x
Maybe it’s there and I’ve missed it or I haven’t come across it yet But I’m wondering how to move the config for my toggles outside of the appsettings file into another file just for feature flags.
A reference to an article or tutorial is what I’m looking for. Thanks
EDIT 1
This is what my Startup.cs has:
public void ConfigureServices(IServiceCollection services)
{
var appSettings = new AppSettings
{
ConnectionString = Configuration["AppSettings:ConnectionString"],
Environment = Configuration["AppSettings:Environment"],
EncryptionPassword = Configuration["AppSettings:EncryptionPassword"]
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString())
};
services.AddFeatureManagement(Configuration.GetSection("FeatureManagement")).AddFeatureFilter<WidgetsFeaturesFilter>();
EDIT 2
I moved the feature management section from appsettings.json into a new file in the same location / folder as appsettings.json called "FeatureFlags.json". But when I build, and try to call a method that has a feature toggle check, I see this warning:
Warn: Microsoft.FeatureManagement.FeatureManager[0] The feature declaration for the feature 'WidgetsApi' was not found. Microsoft.FeatureManagement.FeatureManager: Warning: The feature declaration for the feature 'WidgetsApi' was not found.
Here's all the code:
FeatureFlags.json
{
"FeatureManagement": {
"AddWidgets": {
"EnabledFor": [
{
"Name": "Widgets2Features"
}
]
},
"WidgetsApi": {
"EnabledFor": [
{
"Name": "Widgets2Features"
}
]
}
}
}
WidgetsFeaturesFilter.cs
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;
namespace My.Server.Features
{
[FilterAlias("Widgets2Features")]
public class WidgetsFeaturesFilter : IFeatureFilter
{
private readonly IServiceScopeFactory scopeFactory;
public WidgetsFeaturesFilter(IServiceScopeFactory scopeFactory)
{
this.scopeFactory = scopeFactory;
}
public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context)
{
using var scope = scopeFactory.CreateScope();
var featureRepo = scope.ServiceProvider.GetService<IGenericRepository<WidgetFeatures>>();
var feature = featureRepo.Retrieve(filter: "[Name] = @FeatureName", filterParameters: new { context.FeatureName }).FirstOrDefault();
return Task.FromResult(feature != null && feature.Enabled);
}
}
}
Program.cs
using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace My.Server
{
public class Program
{
public static void Main(string[] args)
{
Logging.Serilog.Initialize();
try
{
Log.Information($"Starting web host on process id { Process.GetCurrentProcess().Id }");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
builder.AddJsonFile("FeatureFlags.json");
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
CodePudding user response:
You can create a JSON file with feature flags and include it in the configuration system. Here is the code.
If you're using ASP.NET Core 6.0 - Modify the Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("FeatureFlags.json");
And if you're using ASP.NET Core 5.0 - Modify the Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
builder.AddJsonFile("FeatureFlags.json");
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});