Unable to resolve service for type Microsoft Extensions Configuration IConfiguration

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 08:24:05

问题


I am getting this error, could not understand for the life of me.

Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate 'Microsoft.FeatureManagement.ConfigurationFeatureSettingsProvider'.

This is a simple .net core 2.2 console app, with the following nuget packages added.

  1. Microsoft.Extensions.Configuration.Json
  2. Microsoft.Extensions.DependencyInjection
  3. 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



回答1:


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

Note the generic <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.



来源:https://stackoverflow.com/questions/59332463/unable-to-resolve-service-for-type-microsoft-extensions-configuration-iconfigura

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!