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
* Is registered as a [singleton service](/dotnet/core/extensions/dependency-injection#singleton) and can be injected into any [service lifetime](/dotnet/core/extensions/dependency-injection#service-lifetimes).
465
+
* Is registered as a [singleton service](/dotnet/core/extensions/dependency-injection/service-lifetimes#singleton) and can be injected into any [service lifetime](/dotnet/core/extensions/dependency-injection/service-lifetimes).
* Covered later in this article in the [Use `IOptionsSnapshot` to read updated data](#use-ioptionssnapshot-to-read-updated-data) section.
470
470
* Is useful in scenarios where options should be recomputed on every request.
471
-
* Is registered as a [scoped service](/dotnet/core/extensions/dependency-injection#scoped), so it can't be injected into a singleton service.
471
+
* Is registered as a [scoped service](/dotnet/core/extensions/dependency-injection/service-lifetimes#scoped), so it can't be injected into a singleton service.
* Covered later in this article in the [Use `IOptionsMonitor` to read updated data](#use-ioptionssnapshot-to-read-updated-data) section.
477
477
* Is used to retrieve options and manage options notifications for `TOptions` instances.
478
-
* Is registered as a [singleton service](/dotnet/core/extensions/dependency-injection#singleton) and can be injected into any [service lifetime](/dotnet/core/extensions/dependency-injection#service-lifetimes).
478
+
* Is registered as a [singleton service](/dotnet/core/extensions/dependency-injection/service-lifetimes#singleton) and can be injected into any [service lifetime](/dotnet/core/extensions/dependency-injection/service-lifetimes).
@@ -491,13 +491,13 @@ For the preceding code, changes to the JSON configuration in the app settings fi
491
491
Using <xref:Microsoft.Extensions.Options.IOptionsSnapshot%601>:
492
492
493
493
* Options are computed once per request when accessed and cached for the lifetime of the request.
494
-
* May incur a significant performance penalty because it's a [scoped service](/dotnet/core/extensions/dependency-injection#scoped) and is recomputed per request. For more information, see [`IOptionsSnapshot` is very slow (`dotnet/runtime` #53793)](https://github.com/dotnet/runtime/issues/53793) and [Improve the performance of configuration binding (`dotnet/runtime` #36130)](https://github.com/dotnet/runtime/issues/36130).
494
+
* May incur a significant performance penalty because it's a [scoped service](/dotnet/core/extensions/dependency-injection/service-lifetimes#scoped) and is recomputed per request. For more information, see [`IOptionsSnapshot` is very slow (`dotnet/runtime` #53793)](https://github.com/dotnet/runtime/issues/53793) and [Improve the performance of configuration binding (`dotnet/runtime` #36130)](https://github.com/dotnet/runtime/issues/36130).
495
495
* Changes to the configuration are read after the app starts when using configuration providers that support reading updated configuration values.
496
496
497
497
The difference between [`IOptionsMonitor`](#use-ioptionsmonitor-to-read-updated-data) and <xref:Microsoft.Extensions.Options.IOptionsSnapshot%601> is that:
498
498
499
-
* <xref:Microsoft.Extensions.Options.IOptionsMonitor%601> is a [singleton service](/dotnet/core/extensions/dependency-injection#singleton) that retrieves current option values at any time, which is especially useful in singleton dependencies.
500
-
* <xref:Microsoft.Extensions.Options.IOptionsSnapshot%601> is a [scoped service](/dotnet/core/extensions/dependency-injection#scoped) and provides a snapshot of the options at the time the `IOptionsSnapshot<T>` object is constructed. Options snapshots are designed for use with transient and scoped dependencies.
499
+
* <xref:Microsoft.Extensions.Options.IOptionsMonitor%601> is a [singleton service](/dotnet/core/extensions/dependency-injection/service-lifetimes#singleton) that retrieves current option values at any time, which is especially useful in singleton dependencies.
500
+
* <xref:Microsoft.Extensions.Options.IOptionsSnapshot%601> is a [scoped service](/dotnet/core/extensions/dependency-injection/service-lifetimes#scoped) and provides a snapshot of the options at the time the `IOptionsSnapshot<T>` object is constructed. Options snapshots are designed for use with transient and scoped dependencies.
501
501
502
502
The ASP.NET Core runtime uses <xref:Microsoft.Extensions.Options.OptionsCache%601> to cache the options instance after it's created.
503
503
@@ -620,8 +620,8 @@ After the app has started, changes to the JSON configuration in the app settings
620
620
621
621
The difference between <xref:Microsoft.Extensions.Options.IOptionsMonitor%601> and [`IOptionsSnapshot`](#use-ioptionssnapshot-to-read-updated-data) is that:
622
622
623
-
* <xref:Microsoft.Extensions.Options.IOptionsMonitor%601> is a [singleton service](/dotnet/core/extensions/dependency-injection#singleton) that retrieves current option values at any time, which is especially useful in singleton dependencies.
624
-
* <xref:Microsoft.Extensions.Options.IOptionsSnapshot%601> is a [scoped service](/dotnet/core/extensions/dependency-injection#scoped) and provides a snapshot of the options at the time the `IOptionsSnapshot<T>` object is constructed. Options snapshots are designed for use with transient and scoped dependencies.
623
+
* <xref:Microsoft.Extensions.Options.IOptionsMonitor%601> is a [singleton service](/dotnet/core/extensions/dependency-injection/service-lifetimes#singleton) that retrieves current option values at any time, which is especially useful in singleton dependencies.
624
+
* <xref:Microsoft.Extensions.Options.IOptionsSnapshot%601> is a [scoped service](/dotnet/core/extensions/dependency-injection/service-lifetimes#scoped) and provides a snapshot of the options at the time the `IOptionsSnapshot<T>` object is constructed. Options snapshots are designed for use with transient and scoped dependencies.
625
625
626
626
<xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601> is used by <xref:Microsoft.Extensions.Options.IOptionsMonitor%601> to cache `TOptions` instances. <xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601.TryRemove%2A?displayProperty=nameWithType> invalidates options instances in the monitor so that the value is recomputed. Values can be manually introduced with <xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601.TryAdd%2A?displayProperty=nameWithType>. The <xref:Microsoft.Extensions.Options.IOptionsMonitorCache%601.Clear%2A> method is used when all named instances should be recreated on demand.
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/dependency-injection.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,7 +109,7 @@ The following code is generated by the Razor Pages template using individual acc
109
109
110
110
## Service lifetimes
111
111
112
-
See [Service lifetimes](/dotnet/core/extensions/dependency-injection#service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
112
+
See [Service lifetimes](/dotnet/core/extensions/dependency-injection/service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
113
113
114
114
To use scoped services in middleware, use one of the following approaches:
115
115
@@ -120,7 +120,7 @@ For more information, see <xref:fundamentals/middleware/write#per-request-middle
120
120
121
121
## Service registration methods
122
122
123
-
See [Service registration methods](/dotnet/core/extensions/dependency-injection#service-registration-methods) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
123
+
See [Service registration](/dotnet/core/extensions/dependency-injection/service-registration) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
124
124
125
125
It's common to use multiple implementations when [mocking types for testing](xref:test/integration-tests#inject-mock-services).
126
126
@@ -270,15 +270,15 @@ In the preceding code:
270
270
271
271
### IDisposable guidance for Transient and shared instances
272
272
273
-
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
273
+
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection/guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
274
274
275
275
## Default service container replacement
276
276
277
-
See [Default service container replacement](/dotnet/core/extensions/dependency-injection-guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
277
+
See [Default service container replacement](/dotnet/core/extensions/dependency-injection/guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
278
278
279
279
## Recommendations
280
280
281
-
See [Recommendations](/dotnet/core/extensions/dependency-injection-guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
281
+
See [Recommendations](/dotnet/core/extensions/dependency-injection/guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
282
282
283
283
* Avoid using the *service locator pattern*. For example, don't invoke <xref:System.IServiceProvider.GetService%2A> to obtain a service instance when you can use DI instead:
284
284
@@ -349,11 +349,11 @@ The following table lists a small sample of these framework-registered services:
349
349
*[NDC Conference Patterns for DI app development](https://www.youtube.com/watch?v=x-C-CNBVTaY)
350
350
*<xref:fundamentals/startup>
351
351
*<xref:fundamentals/middleware/extensibility>
352
-
*[Understand dependency injection basics in .NET](/dotnet/core/extensions/dependency-injection-basics)
*[ASP.NET CORE DEPENDENCY INJECTION: WHAT IS THE ISERVICECOLLECTION?](https://www.stevejgordon.co.uk/aspnet-core-dependency-injection-what-is-the-iservicecollection)
356
+
*[ASP.NET Core Dependecy Injection: What is the IServiceCollection?](https://www.stevejgordon.co.uk/aspnet-core-dependency-injection-what-is-the-iservicecollection)
357
357
*[Four ways to dispose IDisposables in ASP.NET Core](https://andrewlock.net/four-ways-to-dispose-idisposables-in-asp-net-core/)
358
358
*[Writing Clean Code in ASP.NET Core with Dependency Injection (MSDN)](/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection)
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/dependency-injection/includes/dependency-injection-5-7.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,7 +99,7 @@ The following code is generated by the Razor Pages template using individual use
99
99
100
100
## Service lifetimes
101
101
102
-
See [Service lifetimes](/dotnet/core/extensions/dependency-injection#service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
102
+
See [Service lifetimes](/dotnet/core/extensions/dependency-injection/service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
103
103
104
104
To use scoped services in middleware, use one of the following approaches:
105
105
@@ -246,15 +246,15 @@ In the preceding code:
246
246
247
247
### IDisposable guidance for Transient and shared instances
248
248
249
-
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
249
+
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection/guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
250
250
251
251
## Default service container replacement
252
252
253
-
See [Default service container replacement](/dotnet/core/extensions/dependency-injection-guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
253
+
See [Default service container replacement](/dotnet/core/extensions/dependency-injection/guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
254
254
255
255
## Recommendations
256
256
257
-
See [Recommendations](/dotnet/core/extensions/dependency-injection-guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
257
+
See [Recommendations](/dotnet/core/extensions/dependency-injection/guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
258
258
259
259
* Avoid using the *service locator pattern*. For example, don't invoke <xref:System.IServiceProvider.GetService%2A> to obtain a service instance when you can use DI instead:
260
260
@@ -489,7 +489,7 @@ The following code is generated by the Razor Pages template using individual use
489
489
490
490
## Service lifetimes
491
491
492
-
See [Service lifetimes](/dotnet/core/extensions/dependency-injection#service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
492
+
See [Service lifetimes](/dotnet/core/extensions/dependency-injection/service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
493
493
494
494
To use scoped services in middleware, use one of the following approaches:
495
495
@@ -640,15 +640,15 @@ In the preceding code:
640
640
641
641
### IDisposable guidance for Transient and shared instances
642
642
643
-
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
643
+
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection/guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
644
644
645
645
## Default service container replacement
646
646
647
-
See [Default service container replacement](/dotnet/core/extensions/dependency-injection-guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
647
+
See [Default service container replacement](/dotnet/core/extensions/dependency-injection/guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
648
648
649
649
## Recommendations
650
650
651
-
See [Recommendations](/dotnet/core/extensions/dependency-injection-guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
651
+
See [Recommendations](/dotnet/core/extensions/dependency-injection/guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
652
652
653
653
* Avoid using the *service locator pattern*. For example, don't invoke <xref:System.IServiceProvider.GetService%2A> to obtain a service instance when you can use DI instead:
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/dependency-injection/includes/dependency-injection-8.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ The following code is generated by the Razor Pages template using individual acc
100
100
101
101
## Service lifetimes
102
102
103
-
See [Service lifetimes](/dotnet/core/extensions/dependency-injection#service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
103
+
See [Service lifetimes](/dotnet/core/extensions/dependency-injection/service-lifetimes) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
104
104
105
105
To use scoped services in middleware, use one of the following approaches:
106
106
@@ -252,15 +252,15 @@ In the preceding code:
252
252
253
253
### IDisposable guidance for Transient and shared instances
254
254
255
-
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection-guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
255
+
See [IDisposable guidance for Transient and shared instance](/dotnet/core/extensions/dependency-injection/guidelines#idisposable-guidance-for-transient-and-shared-instances) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
256
256
257
257
## Default service container replacement
258
258
259
-
See [Default service container replacement](/dotnet/core/extensions/dependency-injection-guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
259
+
See [Default service container replacement](/dotnet/core/extensions/dependency-injection/guidelines#default-service-container-replacement) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
260
260
261
261
## Recommendations
262
262
263
-
See [Recommendations](/dotnet/core/extensions/dependency-injection-guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
263
+
See [Recommendations](/dotnet/core/extensions/dependency-injection/guidelines#recommendations) in [Dependency injection in .NET](/dotnet/core/extensions/dependency-injection)
264
264
265
265
* Avoid using the *service locator pattern*. For example, don't invoke <xref:System.IServiceProvider.GetService%2A> to obtain a service instance when you can use DI instead:
266
266
@@ -352,9 +352,9 @@ The following table lists a small sample of these framework-registered services:
352
352
*[NDC Conference Patterns for DI app development](https://www.youtube.com/watch?v=x-C-CNBVTaY)
353
353
*<xref:fundamentals/startup>
354
354
*<xref:fundamentals/middleware/extensibility>
355
-
*[Understand dependency injection basics in .NET](/dotnet/core/extensions/dependency-injection-basics)
*[ASP.NET CORE DEPENDENCY INJECTION: WHAT IS THE ISERVICECOLLECTION?](https://www.stevejgordon.co.uk/aspnet-core-dependency-injection-what-is-the-iservicecollection)
360
360
*[Four ways to dispose IDisposables in ASP.NET Core](https://andrewlock.net/four-ways-to-dispose-idisposables-in-asp-net-core/)
0 commit comments