Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .openpublishing.redirection.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@
"source_path_from_root": "/docs/core/extensions/culture-insensitive-string-operations.md",
"redirect_url": "/dotnet/core/extensions/performing-culture-insensitive-string-operations"
},
{
"source_path_from_root": "/docs/core/extensions/dependency-injection.md",
"redirect_url": "/dotnet/core/extensions/dependency-injection/overview"
},
{
"source_path_from_root": "/docs/core/extensions/dependency-injection-basics.md",
"redirect_url": "/dotnet/core/extensions/dependency-injection/basics"
},
{
"source_path_from_root": "/docs/core/extensions/dependency-injection-guidelines.md",
"redirect_url": "/dotnet/core/extensions/dependency-injection/guidelines"
},
{
"source_path_from_root": "/docs/core/extensions/dependency-injection-usage.md",
"redirect_url": "/dotnet/core/extensions/dependency-injection/usage"
},
{
"source_path_from_root": "/docs/core/getting-started.md",
"redirect_url": "/dotnet/core/get-started",
Expand Down
2 changes: 1 addition & 1 deletion docs/ai/how-to/access-data-in-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ If you manually invoke an <xref:Microsoft.Extensions.AI.AIFunction> by calling <

- A dictionary of named arguments.
- <xref:Microsoft.Extensions.AI.AIFunctionArguments.Context>: An arbitrary `IDictionary<object, object>` for passing additional ambient data into the function.
- <xref:Microsoft.Extensions.AI.AIFunctionArguments.Services>: An <xref:System.IServiceProvider> that lets the `AIFunction` resolve arbitrary state from a [dependency injection (DI)](../../core/extensions/dependency-injection.md) container.
- <xref:Microsoft.Extensions.AI.AIFunctionArguments.Services>: An <xref:System.IServiceProvider> that lets the `AIFunction` resolve arbitrary state from a [dependency injection (DI)](../../core/extensions/dependency-injection/overview.md) container.

If you want to access either the `AIFunctionArguments` or the `IServiceProvider` from within your <xref:Microsoft.Extensions.AI.AIFunctionFactory.Create*?displayProperty=nameWithType> delegate, create a parameter typed as `IServiceProvider` or `AIFunctionArguments`. That parameter will be bound to the relevant data from the `AIFunctionArguments` passed to `AIFunction.InvokeAsync()`.

Expand Down
2 changes: 1 addition & 1 deletion docs/ai/ichatclient.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ For scenarios where you need a different implementation for `GetResponseAsync` a

## Dependency injection

<xref:Microsoft.Extensions.AI.IChatClient> implementations are often provided to an application via [dependency injection (DI)](../core/extensions/dependency-injection.md). In the following example, an <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> is added into the DI container, as is an `IChatClient`. The registration for the `IChatClient` uses a builder that creates a pipeline containing a caching client (which then uses an `IDistributedCache` retrieved from DI) and the sample client. The injected `IChatClient` can be retrieved and used elsewhere in the app.
<xref:Microsoft.Extensions.AI.IChatClient> implementations are often provided to an application via [dependency injection (DI)](../core/extensions/dependency-injection/overview.md). In the following example, an <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> is added into the DI container, as is an `IChatClient`. The registration for the `IChatClient` uses a builder that creates a pipeline containing a caching client (which then uses an `IDistributedCache` retrieved from DI) and the sample client. The injected `IChatClient` can be retrieved and used elsewhere in the app.

:::code language="csharp" source="snippets/microsoft-extensions-ai/ConsoleAI.DependencyInjection/Program.cs":::

Expand Down
2 changes: 1 addition & 1 deletion docs/ai/microsoft-extensions-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ For more samples, see the [dotnet/ai-samples](https://aka.ms/meai-samples) GitHu

- [Request a response with structured output](./quickstarts/structured-output.md)
- [Build an AI chat app with .NET](./quickstarts/build-chat-app.md)
- [Dependency injection in .NET](../core/extensions/dependency-injection.md)
- [Dependency injection in .NET](../core/extensions/dependency-injection/overview.md)
- [Caching in .NET](../core/extensions/caching.md)
- [Rate limit an HTTP handler in .NET](../core/extensions/http-ratelimiter.md)
10 changes: 5 additions & 5 deletions docs/architecture/maui/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ private readonly IAppEnvironmentService _appEnvironmentService;

public ProfileViewModel(
IAppEnvironmentService appEnvironmentService,
IDialogService dialogService,
INavigationService navigationService,
IDialogService dialogService,
INavigationService navigationService,
ISettingsService settingsService)
: base(dialogService, navigationService, settingsService)
{
Expand All @@ -51,7 +51,7 @@ There are several advantages to using a dependency injection container:

In the context of a .NET MAUI app that uses MVVM, a dependency injection container will typically be used for registering and resolving views, registering and resolving view models, and for registering services and injecting them into view models.

There are many dependency injection containers available in .NET; the eShop multi-platform app uses `Microsoft.Extensions.DependencyInjection` to manage the instantiation of views, view models, and service classes in the app. `Microsoft.Extensions.DependencyInjection` facilitates building loosely coupled apps, and provides all of the features commonly found in dependency injection containers, including methods to register type mappings and object instances, resolve objects, manage object lifetimes, and inject dependent objects into constructors of objects that it resolves. For more information about `Microsoft.Extensions.DependencyInjection`, see [Dependency injection in .NET](../../core/extensions/dependency-injection.md).
There are many dependency injection containers available in .NET; the eShop multi-platform app uses `Microsoft.Extensions.DependencyInjection` to manage the instantiation of views, view models, and service classes in the app. `Microsoft.Extensions.DependencyInjection` facilitates building loosely coupled apps, and provides all of the features commonly found in dependency injection containers, including methods to register type mappings and object instances, resolve objects, manage object lifetimes, and inject dependent objects into constructors of objects that it resolves. For more information about `Microsoft.Extensions.DependencyInjection`, see [Dependency injection in .NET](../../core/extensions/dependency-injection/overview.md).

In .NET MAUI, the `MauiProgram` class will call into the `CreateMauiApp` method to create a `MauiAppBuilder` object. The `MauiAppBuilder` object has a `Services` property of type `IServiceCollection`, which provides a place to register our components, such as views, view models, and services for dependency injection. Any components registered with the `Services` property will be provided to the dependency injection container when the `MauiAppBuilder.Build` method is called.

Expand Down Expand Up @@ -82,7 +82,7 @@ public static class MauiProgram
public static MauiApp CreateMauiApp()
=> MauiApp.CreateBuilder()
.UseMauiApp<App>()
// Omitted for brevity
// Omitted for brevity
.RegisterAppServices()
.RegisterViewModels()
.RegisterViews()
Expand Down Expand Up @@ -182,7 +182,7 @@ public partial class FiltersView : ContentPage
```

> [!TIP]
> The dependency injection container is great for creating view model instances. If a view model has dependencies, it will handle the creation and injection of any required services. Just make sure that you register your view models and any dependencies that they may have with the `CreateMauiApp` method in the `MauiProgram` class.
> The dependency injection container is great for creating view model instances. If a view model has dependencies, it will handle the creation and injection of any required services. Just make sure that you register your view models and any dependencies that they may have with the `CreateMauiApp` method in the `MauiProgram` class.

## Summary

Expand Down
12 changes: 6 additions & 6 deletions docs/azure/sdk/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ms.date: 04/25/2025

# Dependency injection with the Azure SDK for .NET

This article demonstrates how to register Azure service clients from the [latest Azure client libraries for .NET](https://azure.github.io/azure-sdk/releases/latest/index.html#net) for [dependency injection in a .NET app](../../core/extensions/dependency-injection.md). Every modern .NET app starts up by using the instructions provided in a *Program.cs* file.
This article demonstrates how to register Azure service clients from the [latest Azure client libraries for .NET](https://azure.github.io/azure-sdk/releases/latest/index.html#net) for [dependency injection in a .NET app](../../core/extensions/dependency-injection/overview.md). Every modern .NET app starts up by using the instructions provided in a *Program.cs* file.

## Install packages

Expand Down Expand Up @@ -66,7 +66,7 @@ In the preceding code:

## Use the registered clients

With the clients registered, as described in the [Register clients and subclients](#register-clients-and-subclients) section, you can now use them. In the following example, [constructor injection](../../core/extensions/dependency-injection.md#constructor-injection-behavior) is used to obtain the Blob Storage client and a factory for Service Bus sender subclients in an ASP.NET Core API controller:
With the clients registered, as described in the [Register clients and subclients](#register-clients-and-subclients) section, you can now use them. In the following example, [constructor injection](../../core/extensions/dependency-injection/overview.md#constructor-injection-behavior) is used to obtain the Blob Storage client and a factory for Service Bus sender subclients in an ASP.NET Core API controller:

```csharp
[ApiController]
Expand All @@ -75,19 +75,19 @@ public class MyApiController : ControllerBase
{
private readonly BlobServiceClient _blobServiceClient;
private readonly ServiceBusSender _serviceBusSender;

public MyApiController(
BlobServiceClient blobServiceClient,
IAzureClientFactory<ServiceBusSender> senderFactory)
{
_blobServiceClient = blobServiceClient;
_serviceBusSender = senderFactory.CreateClient("myQueueName");
}

[HttpGet]
public async Task<IEnumerable<string>> Get()
{
BlobContainerClient containerClient =
BlobContainerClient containerClient =
_blobServiceClient.GetBlobContainerClient("demo");
var results = new List<string>();

Expand Down Expand Up @@ -270,7 +270,7 @@ At some point, you may want to change the default settings for a service client.
```

You can change the retry policy to suit your needs like so:

```csharp
builder.Services.AddAzureClients(clientBuilder =>
{
Expand Down
2 changes: 1 addition & 1 deletion docs/azure/sdk/thread-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ Further guidance for properly managing and disposing of `HttpClient` instances c
## See also

- [Dependency injection with the Azure SDK for .NET](./dependency-injection.md)
- [Dependency injection in .NET](../../core/extensions/dependency-injection.md)
- [Dependency injection in .NET](../../core/extensions/dependency-injection/overview.md)
2 changes: 1 addition & 1 deletion docs/azure/sdk/unit-testing-mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,6 @@ Here's how it works:

## See also

* [Dependency injection in .NET](../../core/extensions/dependency-injection.md)
* [Dependency injection in .NET](../../core/extensions/dependency-injection/overview.md)
* [Unit testing best practices](../../core/testing/unit-testing-best-practices.md)
* [Unit testing C# in .NET using dotnet test and xUnit](../../core/testing/unit-testing-csharp-with-xunit.md)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Calling `GetKeyedServices()` with `KeyedService.AnyKey` returned all registratio

## New behavior

Starting in .NET 10, calling `GetKeyedService()` with `KeyedService.AnyKey` throws an <xref:System.InvalidOperationException>. This ensures that `AnyKey` can't be used to resolve a single service, as it's [intended to represent a special case](../../../extensions/dependency-injection.md#keyedserviceanykey-property) rather than a specific key.
Starting in .NET 10, calling `GetKeyedService()` with `KeyedService.AnyKey` throws an <xref:System.InvalidOperationException>. This ensures that `AnyKey` can't be used to resolve a single service, as it's [intended to represent a special case](../../../extensions/dependency-injection/overview.md#keyedserviceanykey-property) rather than a specific key.

```csharp
var service = serviceProvider.GetKeyedService(typeof(IMyService), KeyedService.AnyKey);
Expand Down Expand Up @@ -57,4 +57,4 @@ If you use `GetKeyedService()` or `GetKeyedServices()` with `KeyedService.AnyKey

## See also

- [Use KeyedService.AnyKey for fallbacks](../../../extensions/dependency-injection.md#keyedserviceanykey-property)
- [Use KeyedService.AnyKey for fallbacks](../../../extensions/dependency-injection/overview.md#keyedserviceanykey-property)
4 changes: 2 additions & 2 deletions docs/core/extensions/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Since the absolute expiration (<xref:Microsoft.Extensions.Caching.Memory.MemoryC

## Worker Service caching

One common strategy for caching data, is updating the cache independently from the consuming data services. The *Worker Service* template is a great example, as the <xref:Microsoft.Extensions.Hosting.BackgroundService> runs independent (or in the background) from the other application code. When an application starts running that hosts an implementation of the <xref:Microsoft.Extensions.Hosting.IHostedService>, the corresponding implementation (in this case the `BackgroundService` or "worker") start running in the same process. These hosted services are registered with DI as singletons, through the <xref:Microsoft.Extensions.DependencyInjection.ServiceCollectionHostedServiceExtensions.AddHostedService%60%601(Microsoft.Extensions.DependencyInjection.IServiceCollection)> extension method. Other services can be registered with DI with any [service lifetime](dependency-injection.md#service-lifetimes).
One common strategy for caching data, is updating the cache independently from the consuming data services. The *Worker Service* template is a great example, as the <xref:Microsoft.Extensions.Hosting.BackgroundService> runs independent (or in the background) from the other application code. When an application starts running that hosts an implementation of the <xref:Microsoft.Extensions.Hosting.IHostedService>, the corresponding implementation (in this case the `BackgroundService` or "worker") start running in the same process. These hosted services are registered with DI as singletons, through the <xref:Microsoft.Extensions.DependencyInjection.ServiceCollectionHostedServiceExtensions.AddHostedService%60%601(Microsoft.Extensions.DependencyInjection.IServiceCollection)> extension method. Other services can be registered with DI with any [service lifetime](dependency-injection/service-lifetimes.md).

> [!IMPORTANT]
> The service lifetime's are very important to understand. When you call <xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddMemoryCache%2A> to register all of the in-memory caching services, the services are registered as singletons.
Expand Down Expand Up @@ -321,7 +321,7 @@ To delete values in the distributed cache, call one of the remove APIs:

## See also

- [Dependency injection in .NET](dependency-injection.md)
- [Dependency injection in .NET](dependency-injection/overview.md)
- [.NET Generic Host](generic-host.md)
- [Worker Services in .NET](workers.md)
- [Azure for .NET developers](../../azure/index.yml)
Expand Down
2 changes: 1 addition & 1 deletion docs/core/extensions/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Adding a configuration provider overrides previous configuration values. For exa

### Binding

One of the key advantages of using the .NET configuration abstractions is the ability to bind configuration values to instances of .NET objects. For example, the JSON configuration provider can be used to map *appsettings.json* files to .NET objects and is used with [dependency injection](dependency-injection.md). This enables the [options pattern](options.md), which uses classes to provide strongly typed access to groups of related settings. The default binder is reflection-based, but there's a [source generator alternative](configuration-generator.md) that's easy to enable.
One of the key advantages of using the .NET configuration abstractions is the ability to bind configuration values to instances of .NET objects. For example, the JSON configuration provider can be used to map *appsettings.json* files to .NET objects and is used with [dependency injection](dependency-injection/overview.md). This enables the [options pattern](options.md), which uses classes to provide strongly typed access to groups of related settings. The default binder is reflection-based, but there's a [source generator alternative](configuration-generator.md) that's easy to enable.

.NET configuration provides various abstractions. Consider the following interfaces:

Expand Down
2 changes: 1 addition & 1 deletion docs/core/extensions/custom-configuration-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ The preceding code configures the `WidgetOptions` object from the `"WidgetOption
- [Configuration in .NET](configuration.md)
- [Configuration providers in .NET](configuration-providers.md)
- [Options pattern in .NET](options.md)
- [Dependency injection in .NET](dependency-injection.md)
- [Dependency injection in .NET](dependency-injection/overview.md)
2 changes: 1 addition & 1 deletion docs/core/extensions/custom-logging-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ Running this simple application will render color output to the console window s

- [Logging in .NET](logging.md)
- [Logging providers in .NET](logging-providers.md)
- [Dependency injection in .NET](dependency-injection.md)
- [Dependency injection in .NET](dependency-injection/overview.md)
- [High-performance logging in .NET](high-performance-logging.md)
Loading