Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring custom IBooleanToggleValueProvider with dependency injection #154

Open
DKotoski opened this issue Feb 13, 2018 · 3 comments
Open
Labels

Comments

@DKotoski
Copy link

Hello,
In our startup we have something like:

IServiceCollection AddFeatureToggle(this IServiceCollection service, IConfigurationRoot configuration)
{
     var provider = new CustomBooleanToggleValueProvider(configuration);
     service.AddSingleton<ICustomFeatureToggle>(new CustomFeatureToggle() { ToggleValueProvider = provider });
     return service;
}

The CustomBooleanToggleValueProvider inherits IBooleanToggleValueProvider
When I go to the controller and check if feature is toggled with featureToggle.FeatureEnabled it throws an error. I have noticed that the ToggleValueProvider is still AppSettingsProvider instead of the CustomBooleanToggleValueProvider .

The .AddSingleton extension is from the package Microsoft.Extensions.DependencyInjection

@DKotoski
Copy link
Author

DKotoski commented Feb 14, 2018

I found a workaround with this. I made a class that Inherits SimpleFeatureToggle but has a constructor that takes the IBooleanToggleValueProvider and sets it to ToggleValueProvider when constructing and then the CustomFeatureToggle inherits that instead of SimpleSkillProvider. After the change the DI looks like this

public static IServiceCollection AddFeatureToggle(this IServiceCollection service, IConfigurationRoot configuration)
        {
         var provider = new CustomBooleanToggleValueProvider(configuration);
         service
                .AddSingleton<FeatureToggle.IBooleanToggleValueProvider>(new CustomBooleanToggleValueProvider(configuration))
                .AddSingleton<ICustomFeatureToggle>(new CustomFeatureToggle(provider));

            return service;
        }

While this is a solution that works as needed can you please look into how the problem is caused. We suspect that it is something that is caused by the Microsoft.Extensions.DependencyInjection 2.0.0

@jason-roberts jason-roberts added this to the SomeFutureVersion milestone Feb 15, 2018
@jason-roberts
Copy link
Owner

Hi @DKotoski - thanks for creating an issue for this, can you please provide the error and stacktrace you were getting - thanks :)

@DKotoski
Copy link
Author

DKotoski commented Feb 21, 2018

I unfortunately am not able reproduce it soon because that code come and went. If I find some time ill go back trough git history to that moment and send you the exact message. But the error is something about not being able to find the key. It was looking for "FeatureToggle:CustomFeatureToggle" but it was expected to look for "FeatureToggle.CustomFeatureToggle". It was definitely caused from the dependency injection not being able to inject the singleton
new CustomFeatureToggle() { ToggleValueProvider = provider }
Insdead of creating a feature toggle with the custom provider it generated it the same as would do
new SimpleFeatureToggle()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants