I have problem with injecting IOptionsMonitor with Autofac.
Everything works properly with IOptions, but i'm not able to make it work properly using IOptionsMonitor.
For IOptions registration looks as below:
var cfg = _configuration.GetSection("GlobalAppSettings").Get<GlobalAppSettings>();
builder.Register(c => Options.Create(cfg)).SingleInstance();
And now in constructor:
public class ConfigurationReader : IConfigurationReader
{
public GlobalAppSettings GlobalAppSettings { get; }
public ConfigurationReader(IOptions<GlobalAppSettings> _globalAppSettings)
{
GlobalAppSettings = _globalAppSettings.Value;
}
}
But how to make it works with IOptionsMonitor? Is this a way to create it like IOptions using "Options.Create()" ?
You need to add the following Nuget packages in to your project:
Create a new ServiceCollection with the AddOptions() method. Then add the ServiceCollection to your Autofac builder via Populate() method.
var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection().AddOptions();
var builder = new Autofac.ContainerBuilder();
builder.Populate(serviceCollection);
var container = builder.Build();
After, you can inject IOptions, IOptionsSnapshot and IOptionsMonitor.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.