问题描述
我遇到此错误,无法理解我的一生.
I am getting this error, could not understand for the life of me.
尝试激活"Microsoft.FeatureManagement.ConfigurationFeatureSettingsProvider"时,无法解析类型为"Microsoft.Extensions.Configuration.IConfiguration"的服务.
这是一个简单的.net core 2.2控制台应用程序,其中添加了以下nuget程序包.
This is a simple .net core 2.2 console app, with the following nuget packages added.
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.DependencyInjection
- Microsoft.FeatureManagement
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.DependencyInjection
- Microsoft.FeatureManagement
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.FeatureFilters;
namespace ConfigurationConsoleApp
{
class Program
{
static async Task Main(string[] args)
{
const string FeatureName = "Beta";
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var services = new ServiceCollection();
services.AddSingleton(configuration).AddFeatureManagement().AddFeatureFilter<PercentageFilter>().AddFeatureFilter<AccountIdFilter>();
var serviceProvider = services.BuildServiceProvider();
var featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
var enabled = await featureManager.IsEnabledAsync(FeatureName);
Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} ");
}
}
}
// The following are the command for the packages.
dotnet add package Microsoft.Extensions.Configuration.Json --version 2.1.1
dotnet add package Microsoft.Extensions.DependencyInjection --version 2.1.1
dotnet add package Microsoft.FeatureManagement --version 2.0.0-preview-010610001-1263
推荐答案
好吧,这是经过数小时的拔头发.
Ok, here it is after hours of hair pulling.
services.AddSingleton(configuration).AddFeatureManagement().AddFeatureFilter<PercentageFilter>().AddFeatureFilter<AccountIdFilter>();
should be
services.AddSingleton<IConfiguration>(configuration).AddFeatureManagement().AddFeatureFilter<PercentageFilter>().AddFeatureFilter<AccountIdFilter>();
请注意通用<IConfiguration>
我还注意到,将配置对象声明为IConfiguration也可以解决问题.使用var声明配置可解决此问题.代替var使用 IConfiguration .然后问题又消失了.
Also I have noted that declaring configuration object as IConfiguration will also do the trick. Using var to declare configuration is giving the problem. Instead of var use IConfiguration. Then again the problem goes away.
这篇关于无法解析类型为Microsoft Extensions Configuration IConfiguration的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!