santosh kumar patro
santosh kumar patro

Reputation: 8241

Unable to resolve service for type 'Microsoft.FeatureManagement.IFeatureManager' while attempting to activate 'Startup'

I have asp.net core 3.1 web api project. I have added the nuget package: Microsoft.FeatureManagement.AspNetCore

Add the below in the appsettings.local.json:

{
"FeatureManagement": {
    "EnableNewFeature": true
  }
}

Startup.cs

public class Startup
    {
        private readonly IConfiguration configuration;
        private readonly IWebHostEnvironment webHostEnvironment;
        private readonly IFeatureManager featureManager;
        public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment, IFeatureManager featureManager)
        {
            this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this.webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
            this.featureManager = featureManager ?? throw new ArgumentNullException(nameof(featureManager));
        }
    
        public virtual void ConfigureServices(IServiceCollection services) {
       /// Code.Start
       services.AddFeatureManagement();

       /// Code.End
      }
        public async Task Configure(IApplicationBuilder app, L10NCacheInitializationService l10nIniService)
        {
            app.UseIf(await featureManager.IsEnabledAsync(AppKeys.EnableNewFeature), x => x.UseNewFeature());
        }
    }

On validation I came across the below error : Unable to resolve service for type 'Microsoft.FeatureManagement.IFeatureManager' while attempting to activate 'Startup'.

Can anyone help me to resolve this issue?

Upvotes: 1

Views: 2950

Answers (2)

Jan Muncinsky
Jan Muncinsky

Reputation: 4418

If you need to use IFeatureManager in the Startup class for other cases like using inside of the ConfigureServices method, you can use this extension method:

public static class ConfigurationExtensions
{
    public static bool IsFeatureEnabled(this IConfiguration configuration, string feature)
    {
        var featureServices = new ServiceCollection();
        featureServices.AddFeatureManagement(configuration);
        using var provider = featureServices.BuildServiceProvider();
        var manager = provider.GetRequiredService<IFeatureManager>();

        return manager.IsEnabledAsync(feature).GetAwaiter().GetResult();
    }
}

Upvotes: 1

Jamiec
Jamiec

Reputation: 136174

You cannot inject IFeatureManager in to the constructor of Startup because it's not yet registered. Once registered you can get it using app.ApplicationServices.GetRequiredService

With using Microsoft.Extensions.DependencyInjection at the top of your file it would look something like this:

public class Startup
{
    private readonly IConfiguration configuration;
    private readonly IWebHostEnvironment webHostEnvironment;
    public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
    {
        this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
        this.webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
    }
    
    public virtual void ConfigureServices(IServiceCollection services) 
    {
       services.AddFeatureManagement();
    }
    public async Task Configure(IApplicationBuilder app, L10NCacheInitializationService l10nIniService)
    {
        var featureManager = app.ApplicationServices.GetRequiredService<IFeatureManager>();
        app.UseIf(await featureManager.IsEnabledAsync(AppKeys.EnableNewFeature), x => x.UseNewFeature());
    }
}

Upvotes: 3

Related Questions