You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a strongly typed object that you want to bind to the configuration provider:
28
+
Registering your options gives you access to the following from the dependency injection container:
29
+
30
+
-`TOptions` - Same as `IOptions<TOptions>`, it represents configuration data once when the application starts and any changes in configuration will require the application to be restarted. It is unwrapped from the `IOptions<>` interface so that consuming interfaces do not have to force a dependency on the pattern. It is registered in the dependency injection container with a singleton lifetime.
31
+
32
+
-`IOptions<TOptions>` - Represents configuration data once when the application starts and any changes in configuration will require the application to be restarted. It is registered in the dependency injection container with a singleton lifetime.
33
+
34
+
-`IOptionsSnapshot<TOptions>` - Represents configuration on every request. Any changes in configuration while the application is running will be available for new requests without the need to restart the application. It is registered in the dependency injection container as a scoped lifetime.
35
+
36
+
-`IOptionsMonitor<TOptions>` - Is a service used to retrieve options and manage options notifications for TOptions instances. It is registered in the dependency injection container as a singleton lifetime.
37
+
38
+
### Conventional Binding
39
+
40
+
Create a strongly typed objects that you want to bind to the configuration provider:
29
41
30
42
```csharp
31
43
publicclassSampleOptions
@@ -43,18 +55,16 @@ Strongly typed options are registered as described in the [Options pattern](http
The optional parameter `suffix` can be used to indicated a suffix phrase (default: `Options`) that can be removed from the class name while binding. This allows the tooling to match the option class to a configuration section without the suffix phrase.
52
-
53
-
For example, the following JSON segment would be successfully bound to the `SampleOptions` class:
63
+
The library will attempt to match the strongly typed object to a configuration section following a simple convention: Using the type name of the object with and without the suffix `Options`. In the case of our example class, it will be bound to any section in the configuration with the name `Sample` or `SampleOptions`. The following JSON segment would be successfully bound to the `SampleOptions` class:
54
64
55
65
```json
56
66
{
57
-
"Sample": {\\ or "SampleOptions"
67
+
"Sample": {
58
68
"StringVal": "Orange",
59
69
"IntVal": 999,
60
70
"BoolVal": true,
@@ -63,15 +73,59 @@ For example, the following JSON segment would be successfully bound to the `Samp
63
73
}
64
74
```
65
75
66
-
This gives you access to the following from the dependency injection container:
76
+
### Declarative Binding
67
77
68
-
-`IOptions<TOptions>` - Represents configuration data once when the application starts and any changes in configuration will require the application to be restarted. It is registered in the dependency injection container with a singleton lifetime.
78
+
Create strongly typed objects and apply the `AutoBind` attribute to the ones that you want to bind to the configuration provider. There is an optional parameter to specify the keys that you would like to bind to in the configuration:
69
79
70
-
-`TOptions` - Same as `IOptions<TOptions>`, it represents configuration data once when the application starts and any changes in configuration will require the application to be restarted. It is unwrapped from the `IOptions<>` interface so that consuming interfaces do not have to force a dependency on the pattern. It is registered in the dependency injection container with a singleton lifetime.
80
+
```csharp
81
+
[AutoBind("Squirrels", "Settings")]
82
+
publicclassOtherSampleOptions
83
+
{
84
+
publicstringStringVal { get; set; }
85
+
publicint? IntVal { get; set; }
86
+
publicbool? BoolVal { get; set; }
87
+
publicDateTime? DateVal { get; set; }
88
+
}
71
89
72
-
-`IOptionsSnapshot<TOptions>` - Represents configuration on every request. Any changes in configuration while the application is running will be available for new requests without the need to restart the application. It is registered in the dependency injection container as a scoped lifetime.
90
+
[AutoBind]
91
+
publicclassOtherStuff
92
+
{
93
+
publicstringStringVal { get; set; }
94
+
publicint? IntVal { get; set; }
95
+
publicbool? BoolVal { get; set; }
96
+
publicDateTime? DateVal { get; set; }
97
+
}
98
+
```
73
99
74
-
-`IOptionsMonitor<TOptions>` - Is a service used to retrieve options and manage options notifications for TOptions instances. It is registered in the dependency injection container as a singleton lifetime.
100
+
You can scan one or more assemblies for types that have been decorated with the `AutoBind` attribute by using the registration helper `AutoBindOptions()`:
The library will attempt to match all strongly typed objects a configuration section using the default convention unless keys are specified; each key will be attempted in the order they are declared in the attribute. In the following JSON example, the `OtherSampleOptions` object would be bound to the `Settings` section and `OtherStuff` object would be bound to the `OtherStuffOptions` section:
0 commit comments