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
Copy file name to clipboardExpand all lines: articles/azure-functions/functions-dotnet-dependency-injection.md
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
2
title: Use dependency injection in .NET Azure Functions
3
-
description: Learn how to use dependency injection for registering and using services in .NET functions
3
+
description: Learn how to use dependency injection for registering and using services in .NET Azure Functions, including setup, configuration, and best practices.
4
4
5
5
ms.topic: conceptual
6
6
ms.devlang: csharp
7
7
ms.custom: devx-track-csharp, devx-track-dotnet
8
-
ms.date: 03/24/2021
8
+
ms.date: 05/07/2025
9
9
ms.reviewer: jehollan
10
10
---
11
11
# Use dependency injection in .NET Azure Functions
- Support for dependency injection begins with Azure Functions 2.x.
18
18
19
-
- Dependency injection patterns differ depending on whether your C# functions run [in-process](functions-dotnet-class-library.md) or [out-of-process](dotnet-isolated-process-guide.md).
19
+
- Dependency injection patterns differ depending on whether your C# functions run [in-process](functions-dotnet-class-library.md) or [out-of-process](dotnet-isolated-process-guide.md).
20
20
21
21
> [!IMPORTANT]
22
22
> The guidance in this article applies only to [C# class library functions](functions-dotnet-class-library.md), which run in-process with the runtime. This custom dependency injection model doesn't apply to [.NET isolated functions](dotnet-isolated-process-guide.md), which lets you run .NET functions out-of-process. The .NET isolated worker process model relies on regular ASP.NET Core dependency injection patterns. To learn more, see [Dependency injection](dotnet-isolated-process-guide.md#dependency-injection) in the .NET isolated worker process guide.
@@ -33,7 +33,7 @@ Before you can use dependency injection, you must install the following NuGet pa
33
33
34
34
## Register services
35
35
36
-
To register services, create a method to configure and add components to an `IFunctionsHostBuilder` instance. The Azure Functions host creates an instance of `IFunctionsHostBuilder` and passes it directly into your method.
36
+
To register services, create a method to configure and add components to an `IFunctionsHostBuilder` instance. The Azure Functions host creates an instance of `IFunctionsHostBuilder` and passes it directly into your method.
37
37
38
38
> [!WARNING]
39
39
> For function apps running in the Consumption or Premium plans, modifications to configuration values used in triggers can cause scaling errors. Any changes to these properties by the `FunctionsStartup` class results in a function app startup error.
@@ -138,11 +138,11 @@ Application Insights is added by Azure Functions automatically.
138
138
139
139
> [!WARNING]
140
140
> - Don't add `AddApplicationInsightsTelemetry()` to the services collection, which registers services that conflict with services provided by the environment.
141
-
> - Don't register your own `TelemetryConfiguration` or `TelemetryClient` if you are using the built-in Application Insights functionality. If you need to configure your own `TelemetryClient` instance, create one via the injected `TelemetryConfiguration` as shown in [Log custom telemetry in C# functions](functions-dotnet-class-library.md?tabs=v2%2Ccmd#log-custom-telemetry-in-c-functions).
141
+
> - Don't register your own `TelemetryConfiguration` or `TelemetryClient` if you're using the built-in Application Insights functionality. If you need to configure your own `TelemetryClient` instance, create one via the injected `TelemetryConfiguration` as shown in [Log custom telemetry in C# functions](functions-dotnet-class-library.md?tabs=v2%2Ccmd#log-custom-telemetry-in-c-functions).
142
142
143
143
### ILogger\<T\> and ILoggerFactory
144
144
145
-
The host injects `ILogger<T>` and `ILoggerFactory` services into constructors. However, by default these new logging filters are filtered out of the function logs. You need to modify the `host.json` file to opt in to extra filters and categories.
145
+
The host injects `ILogger<T>` and `ILoggerFactory` services into constructors. However, by default these new logging filters are filtered out of the function logs. You need to modify the `host.json` file to opt in to extra filters and categories.
146
146
147
147
The following example demonstrates how to add an `ILogger<HttpTrigger>` with logs that are exposed to the host.
148
148
@@ -202,7 +202,7 @@ If there are other services you want to take a dependency on, [create an issue a
202
202
203
203
### Overriding host services
204
204
205
-
Overriding services provided by the host is currently not supported. If there are services you want to override, [create an issue and propose them on GitHub](https://github.com/azure/azure-functions-host).
205
+
Overriding services provided by the host is currently not supported. If there are services you want to override, [create an issue and propose them on GitHub](https://github.com/azure/azure-functions-host).
206
206
207
207
## Working with options and settings
208
208
@@ -220,6 +220,7 @@ public class MyOptions
220
220
```
221
221
222
222
And a `local.settings.json` file that might structure the custom setting as follows:
0 commit comments