-
-
Notifications
You must be signed in to change notification settings - Fork 1
03. Registering Options
Tim Maes edited this page Oct 15, 2024
·
1 revision
Bindicate simplifies the registration and configuration of options classes using IOptions<T>.
Decorate your options class with [RegisterOptions] and specify the configuration section.
[RegisterOptions("MyOptions")]
public class MyOptions
{
public string Setting { get; set; }
}Ensure the corresponding section exists in your appsettings.json
{
"MyOptions": {
"Setting": "Value"
}
}
When registering services, include the WithOptions method and pass the configuration.
builder.Services
.AddAutowiringForAssembly(Assembly.GetExecutingAssembly())
.WithOptions(configuration)
.Register();Inject IOptions<T> into your services.
public class MyService
{
private readonly MyOptions _options;
public MyService(IOptions<MyOptions> options)
{
_options = options.Value;
}
// Use _options.Setting as needed
}