Is it possible to resolve an instance of IOptions<AppSettings>
from the ConfigureServices
method in Startup? Normally you can use IServiceProvider
to initialize instances but you don't have it at this stage when you are registering services.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(
configuration.GetConfigurationSection(nameof(AppSettings)));
// How can I resolve IOptions<AppSettings> here?
}
Are you looking for something like following? You can take a look at my comments in the code:
You can build a service provider using the
BuildServiceProvider()
method on theIServiceCollection
:You need the
Microsoft.Extensions.DependencyInjection
package for this.In the case where you just need to bind some options in
ConfigureServices
, you can also use theBind
method:This functionality is available through the
Microsoft.Extensions.Configuration.Binder
package.