---
title: "Invocation pipeline with DI"
description: "Understand how to build your invocation pipeline with a DI container."
url: https://docs.icerpc.dev/icerpc/dependency-injection/invocation-pipeline-with-di
docs_index: https://docs.icerpc.dev/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.icerpc.dev/llms.txt).

# Invocation pipeline with DI

Understand how to build your invocation pipeline with a DI container.

## Invocation pipeline with a DI container

Unlike the [dispatch pipeline](https://docs.icerpc.dev/icerpc/dependency-injection/dispatch-pipeline-with-di.md), the invocation pipeline is pretty much the same with or without a DI container. That's because there is no natural DI scope for an invocation: if an invocation executes within a DI scope, this scope comes from another enclosing activity, such as a dispatch that makes this invocation.

> **Note**
>
> IceRPC for C# does not provide any special support for invocations within a DI scope. In particular [IMiddleware](https://docs.icerpc.dev/icerpc/dependency-injection/dispatch-pipeline-with-di.md#middleware-with-injected-services) has no interceptor counterpart.

## Building an invocation pipeline with Microsoft's DI container

You can call [AddIceRpcInvoker](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.InvokerServiceCollectionExtensions.html#IceRpc_Extensions_DependencyInjection_InvokerServiceCollectionExtensions_AddIceRpcInvoker_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_IceRpc_Extensions_DependencyInjection_IInvokerBuilder__) to add a new invoker (invocation pipeline) singleton to your DI container.

For example:

```csharp
// Construct an invocation pipeline using Microsoft's DI container.

using IceRpc.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
...

// Add a new IInvoker singleton configured with an action.
services
    .AddIceRpcClientConnection(new Uri("icerpc://localhost"))
    .AddIceRpcInvoker(builder => builder.Into<ClientConnection>());
```

You must specify a final invoker with the `Into` method. With this example, the new invoker flows into the `ClientConnection` singleton added by [AddIceRpcClientConnection](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.ClientConnectionServiceCollectionExtensions.html).

[AddIceRpcClientConnection](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.ClientConnectionServiceCollectionExtensions.html) accepts the server address as a `Uri` or a [ServerAddress](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.ServerAddress.html) and sets the `ServerAddress` property of [ClientConnectionOptions](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.ClientConnectionOptions.html). If you call the overload without a server address, you must set `ClientConnectionOptions.ServerAddress` yourself, for example with `services.AddOptions<ClientConnectionOptions>().Configure(options => ...)`.

## Installing an interceptor in an IInvokerBuilder

All the interceptors that ship with IceRPC can be used with or without DI, and use [features](https://docs.icerpc.dev/icerpc/invocation/outgoing-request.md#request-features) for communications within an invocation. For instance, the retry interceptor communicates with a connection cache using an [IServerAddressFeature](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Features.IServerAddressFeature.html) to coordinate retries over replicated servers.

These interceptors can be installed into a [Pipeline](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Pipeline.html) or an [IInvokerBuilder](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.IInvokerBuilder.html).

For example:

```csharp
// Construct an invocation pipeline using Microsoft's DI container.

using IceRpc.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
...

// Add a new IInvoker singleton configured with an action.
services
    .AddIceRpcClientConnection(new Uri("icerpc://localhost"))
    .AddIceRpcInvoker(builder =>
        builder
            .UseLogger()
            .Into<ClientConnection>());
```

Here, `UseLogger` is an extension method provided by the `IceRpc.Logger` assembly. This extension method works with any DI container that implements [IServiceProvider](https://learn.microsoft.com/en-us/dotnet/api/system.iserviceprovider), such as Microsoft's DI container and [Simple Injector](https://simpleinjector.org/)'s container.

The implementation of the `UseLogger` method simply retrieves a logger instance from the DI container and then creates a new interceptor with this instance:

```csharp
public static IInvokerBuilder UseLogger(this IInvokerBuilder builder) =>
    builder.ServiceProvider.GetService(typeof(ILogger<LoggerInterceptor>)) is ILogger logger ?
        builder.Use(next => new LoggerInterceptor(next, logger)) :
        throw new InvalidOperationException(
            $"Could not find service of type '{nameof(ILogger<LoggerInterceptor>)}' in the service container.");
```

We recommend you follow the same pattern when you create your own interceptor and provide `Use` extension methods for both `Pipeline` and `IInvokerBuilder`.

> **Note**
>
> Calling the DI container at runtime is typically discouraged—it's the service locator anti-pattern. Here, you should see the `UseLogger` extension method as infrastructure code exempt from this rule.
