Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get FeatureManager in ConfigureServices?

ASP.NET Core 3 Web Server. appsettings.json "FeatureManagement": { "RouteStrategy": false },

I'd like to get configuration and add route in the function. How to get the FeatureManager here to analyze what features IsEnabled?

public void ConfigureServices(IServiceCollection services)
        {
            services.AddFeatureManagement();
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddSingleton<WeatherForecastService>();

            // <---------???
            //
like image 362
ZedZip Avatar asked Sep 15 '25 18:09

ZedZip


1 Answers

If I understand correctly, what you are asking is how to create a FeatureManager without the dependency injection.

This was not possible at the time of your question but it is now possible since v3.1.0 : https://github.com/microsoft/FeatureManagement-Dotnet/releases/tag/3.1.0

You need to just read your configuration and then you can use ConfigurationFeatureDefinitionProvider and FeatureManager.

IConfiguration configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

var featureDefintionProvider = new ConfigurationFeatureDefinitionProvider(configuration);

var featureManager = new FeatureManager(featureDefintionProvider)();

bool isRouteStrategyEnabled = await featureManager.IsEnabledAsync("RouteStrategy");
like image 85
Mickael V. Avatar answered Sep 17 '25 13:09

Mickael V.