Home > Enterprise >  Configure options with ASP NET Core
Configure options with ASP NET Core

Time:09-29

I have options class as below:

public class SomeOptions : RemoteAuthenticationOptions
{
    public string SomeProperty { get; set; }
}

and class that inherit IPostConfigurationOptions for this options configuration:

public class SomeOptionsPostConfigureOptions : IPostConfigureOptions<SomeOptions>
    {
        private readonly IDataProtectionProvider _dataProtectionProvider;
    
        public SomeOptionsPostConfigureOptions(IDataProtectionProvider dataProtectionProvider)
        {
            _dataProtectionProvider = dataProtectionProvider;
        }
    
        public void PostConfigure(string name, SomeOptions options)
        {
            options.DataProtectionProvider ??= _dataProtectionProvider;
            // and other code...
        }
    }

then I use this configuration in Program.cs as here:

services.ConfigureOptions<SomeOptions>();

When I run this app, I get an exception:

    System.InvalidOperationException: 'No IConfigureOptions<>, IPostConfigureOptions<>, 
or IValidateOptions<> implementations were found.'

Why? There is an configuration implementation for proper options, so I really don't know what I am doing wrong.

CodePudding user response:

Did you mean ConfigureOptions<SomeOptionsPostConfigureOptions>()? That will look at the specified type for known interfaces and register them with the container.

  • Related