Skip to content

Commit 97e6d47

Browse files
Merge pull request #314593 from ecfan/dependency-inject
[Azure Logic Apps] Custom .NET code - Add info about dependency injection and NuGet packet management
2 parents 2f94565 + 814df14 commit 97e6d47

1 file changed

Lines changed: 107 additions & 4 deletions

File tree

articles/logic-apps/create-run-custom-code-functions.md

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ services: azure-logic-apps
55
ms.suite: integration
66
ms.reviewers: estfan, kewear, azla
77
ms.topic: how-to
8+
ai.usage: ai-assisted
89
ms.update-cycle: 1095-days
9-
ms.date: 04/07/2026
10+
ms.date: 04/10/2026
1011
ms.custom:
1112
- devx-track-dotnet
1213
- sfi-image-nochange
@@ -323,7 +324,7 @@ After you confirm that your code compiles and your logic app project contains th
323324

324325
| Operation | Description |
325326
|-----------|-------------|
326-
| Trigger | The built-in [Request trigger named **When a HTTP request is received**](../connectors/connectors-native-reqres.md). |
327+
| Trigger | The built-in [Request trigger named **When an HTTP request is received**](../connectors/connectors-native-reqres.md). |
327328
| Action | The built-in action named **Call a local function in this logic app**. |
328329
| Action | The built-in [Response action named **Response**](../connectors/connectors-native-reqres.md) that you use to reply to the caller only when you use the **Request** trigger. |
329330

@@ -402,6 +403,108 @@ Deploy your custom functions the same way you deploy your logic app project. Whe
402403

403404
For more information, see [Deploy Standard workflows from Visual Studio Code to Azure](create-single-tenant-workflows-visual-studio-code.md#deploy-azure).
404405

406+
## Dependency injection
407+
408+
When you choose **.NET 8**, custom .NET code in Standard workflows supports *dependency injection (DI)*. This capability lets you register services once, which makes them automatically available to your custom code functions at runtime, rather than create dependencies inside each function.
409+
410+
> [!NOTE]
411+
>
412+
> Only .NET 8 custom code projects in Visual Studio Code support dependency injection.
413+
414+
Without dependency injection, custom code functions often:
415+
416+
- Create service instances directly in the function.
417+
- Duplicate logic across multiple functions or workflows.
418+
- Mix business logic with setup and configuration code.
419+
420+
As workflows grow, custom code becomes harder to test, reuse, and maintain. With dependency injection, you can:
421+
422+
- Separate business logic from workflow execution.
423+
- Reuse shared services across multiple custom code functions.
424+
- Align custom code with standard .NET development patterns.
425+
426+
Custom code becomes more manageable in production workflows, especially when multiple workflows rely on the same logic.
427+
428+
### When to use dependency injection
429+
430+
If you have simple or one-off custom code functions, you probably don't need dependency injection. However, if your custom code has the following requirements, you might need to use dependency injection:
431+
432+
- Multiple workflows use or share the same custom code functions.
433+
- Your custom code functions contain business or routing logic that changes over time.
434+
- You require better testability or long‑term maintainability.
435+
436+
### How does dependency injection affect custom .NET functions
437+
438+
Dependency injection doesn't change how you call custom .NET functions or your workflow behavior. This capability only changes the underlying custom code structure but produces the same outcome. The following steps describe this process:
439+
440+
1. Azure Logic Apps loads your custom code project.
441+
1. Azure Logic Apps instantiates, registers, and injects the required services into the function.
442+
1. The function runs using the injected dependencies.
443+
444+
### Enable dependency injection
445+
446+
To use dependency injection with your custom .NET code, complete the following requirements:
447+
448+
1. When you create your custom code project, select **.NET 8**.
449+
450+
Only .NET 8 custom code projects support dependency injection.
451+
452+
1. In your project, add a `StartupConfiguration` class to define the list of dependencies. Implement the `IConfigureStartup` interface and register your dependencies by using `IServiceCollection`, for example:
453+
454+
```csharp
455+
using Microsoft.Azure.Functions.Extensions.Workflows;
456+
using Microsoft.Extensions.DependencyInjection;
457+
458+
public class StartupConfiguration : IConfigureStartup
459+
{
460+
/// <summary>
461+
/// Configures services for the custom code function to use.
462+
/// </summary>
463+
/// <param name="services">The service collection to configure.</param>
464+
public void Configure(IServiceCollection services)
465+
{
466+
// Register the routing service with dependency injection
467+
services.AddSingleton<IRoutingService, OrderRoutingService>();
468+
services.AddSingleton<IDiscountService, DiscountService>();
469+
}
470+
}
471+
```
472+
473+
The `IConfigureStartup` interface is defined in `Microsoft.Extensions.DependencyInjection`. For more information, see [StartupConfiguration.cs](https://github.com/wsilveiranz/CustomCode-Dependency-Injection/blob/master/OrderRouter/StartupConfiguration.cs)
474+
475+
1. In your custom code function class constructor, initialize the registered services by defining them as constructor parameters, rather than creating them inside the function, for example:
476+
477+
```csharp
478+
public class MySampleFunction
479+
{
480+
private readonly ILogger<MySampleFunction> logger;
481+
private readonly IRoutingService routingService;
482+
private readonly IDiscountService discountService;
483+
484+
public MySampleFunction(ILoggerFactory loggerFactory, IRoutingService routingService, IDiscountService discountService)
485+
{
486+
this.logger = loggerFactory.CreateLogger<MySampleFunction>();
487+
this.routingService = routingService;
488+
this.discountService = discountService;
489+
}
490+
491+
// Add your function logic here
492+
493+
}
494+
```
495+
496+
Beyond building and deploying your custom code project, you don't need to take any other steps, edit your workflow, or make any other setup changes in Azure Logic Apps to enable dependency injection.
497+
498+
For more information, see the [Custom Code Dependency Injection sample](https://github.com/wsilveiranz/CustomCode-Dependency-Injection/tree/master/FourthCoffeeServices/FourthCoffeeOrder).
499+
500+
## Bring your own NuGet packages
501+
502+
For NuGet-based custom code projects that use .NET 8, you can include and manage your own NuGet packages without having to resolve conflicts with dependencies used by the language worker host. Just directly add the assembly dependencies to the separate assembly location in your project. With the following exceptions, you can bring any .NET 8-compatible dependent assembly versions that your project needs:
503+
504+
- Microsoft.Extensions.Logging.Abstractions
505+
- Microsoft.Extensions.DependencyInjection.Abstractions
506+
- Microsoft.Azure.Functions.Extensions.Workflows.Abstractions
507+
405508
## Troubleshoot problems
406509

407510
### Action information pane error
@@ -426,7 +529,7 @@ If the Output window shows an error similar to the following message, make sure
426529

427530
### Build failures
428531

429-
If your function doesn't include variables, and you build your code, the Output window might show the following error messages:
532+
If your function doesn't include variables and you build your code, the Output window might show the following error messages:
430533

431534
`C:\Users\yourUserName\...\custom-code-project\Function\func.cs (24,64): error CS1031: Type expected [C:\Users\yourUserName\...\custom-code-project\Function\func.csproj]`<br>
432535
`C:\Users\yourUserName\...\custom-code-project\Function\func.cs (24,64): error CS1001: Identifier expected [C:\Users\yourUserName\...\custom-code-project\Function\func.csproj]`
@@ -439,7 +542,7 @@ If your function doesn't include variables, and you build your code, the Output
439542
`0 Warning(s)`<br>
440543
`2 Error(s)`
441544

442-
To fix this problem, in your code's `Run` method, append the following parameter:
545+
To fix this problem, in your code's `Run` method, add the following parameter:
443546

444547
`string parameter1 = null`
445548

0 commit comments

Comments
 (0)