I have ASP.NET Core Web API.
I want to inject in DI container IOptions<MySetting> options
. In Startup
class it injected like this -
services.Configure<MySettings>(Configuration.GetSection("MySetting"));
. Where MySettings
is class with some fields and a section with the same name in appsetings.json
.
Also i want inject it with other way. With reflection:
var configureMethod = typeof(OptionsConfigurationServiceCollectionExtensions).GetMethods()
.Single(m => m.GetParameters().Length == 2)
.MakeGenericMethod(typeof(MySettings));
I sure configureMethod
matches with services.Configure<MySettings>(Configuration.GetSection("MySetting"));
.
But when I try to invoke this:
configureMethod.Invoke(null, new object?[] { services, configuration.GetSection("MySettings") });
I get error:
Unhandled exception. System.ArgumentException: Cannot instantiate implementation type 'System.ComponentModel.Design.DesignerOptionService' for service type 'System.ComponentModel.Design.IDesignerOptionService'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.Populate()
...
What am I doing wrong and how to fix it?