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?
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());
}
}