22

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


VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • 1
    Does this answer your question? [ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate](https://stackoverflow.com/questions/40900414/asp-net-core-dependency-injection-error-unable-to-resolve-service-for-type-whil) – Camilo Terevinto Oct 30 '20 at 13:20

2 Answers2

35

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.

VivekDev
  • 20,868
  • 27
  • 132
  • 202
0

Sorry for commenting this as an answer, but the above code be careful if you are using it inside a new project. It goes still into the root to search for it, might cause issues when you have 2 projects with the same application.Environment.json

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