If using IOptions<T> then update Service constructor to explicitly depend on IOptions<MyOptions> so that it can be injected into the class.
public class Service: IService {
public Service(IOptions<MyOptions> options) {
this.arg1 = options.Value.arg1;
this.arg2 = options.Value.arg2;
}
}
Configuration can be simplified to
services.Configure<MyOptions>(Configuration.GetSection(nameof(MyOptions)));
services.AddTransient<IService, Service>();
Assuming appsettings.json contains
{
"MyOptions": {
"arg1": value1,
"arg2": value2
}
}
If unable to change service class constructor then resolve option in object factory delegate
services.AddTransient<IService, Service>(serviceProvider => {
var options = serviceProvider.GetService<IOptions<MyOptions>>();
return new Service(options.Value.arg1, options.Value.arg2);
});
Reference Options pattern in ASP.NET Core