---
title: "Dependency Injection and IceRPC for C#"
description: "Understand how IceRPC for C# provides support for Dependency Injection (DI)."
url: https://docs.icerpc.dev/icerpc/dependency-injection/di-and-icerpc-for-csharp
docs_index: https://docs.icerpc.dev/llms.txt
---

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

# Dependency Injection and IceRPC for C#

Understand how IceRPC for C# provides support for Dependency Injection (DI).

## DI as an optional feature

The first rule of DI is: don't introduce a dependency on DI.

IceRPC for C# takes this rule to heart by providing full support for DI while making this support completely optional.

The IceRPC C# API was designed to be both DI-friendly and DI container agnostic. Additionally, IceRPC provides support code to help you use IceRPC with [Microsoft's DI container](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection).

## DI APIs

All the DI-related APIs provided by IceRPC are in the same namespace, [IceRpc.Extensions.DependencyInjection](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.html), but are implemented by several assemblies:

- [IceRpc.dll](https://github.com/icerpc/icerpc-csharp/blob/0.6.x/src/IceRpc/README.md) provides abstractions such as [IDispatcherBuilder](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.IDispatcherBuilder.html) and [IInvokerBuilder](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.IInvokerBuilder.html).
- [IceRpc.Deadline.dll](https://github.com/icerpc/icerpc-csharp/blob/0.6.x/src/IceRpc.Deadline/README.md) and other interceptor/middleware assemblies provide extension methods for `IDispatcherBuilder` and `IInvokerBuilder`. These extension methods are DI container agnostic.
- [IceRpc.Extensions.DependencyInjection.dll](https://github.com/icerpc/icerpc-csharp/blob/0.6.x/src/IceRpc.Extensions.DependencyInjection/README.md) provides support code for Microsoft's DI container. This includes various extension methods for [IServiceCollection](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection), such as [AddIceRpcServer](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.ServerServiceCollectionExtensions.html) and [AddIceRpcClientConnection](https://code.icerpc.dev/csharp/0.6.x/api/reference/IceRpc.Extensions.DependencyInjection.ClientConnectionServiceCollectionExtensions.html), and implementations for `IDispatcherBuilder` and `IInvokerBuilder`.

```mermaid
---
title: Assembly dependency graph
---
flowchart BT
    di([Microsoft.Extensions.DependencyInjection.Abstractions.dll])
    logging([Microsoft.Extensions.Logging.Abstractions.dll])
    options([Microsoft.Extensions.Options.dll])
    IceRpc.dll --> logging
    IceRpc.Logger.dll --> IceRpc.dll
    IceRpc.Logger.dll --> logging
    IceRpc.Deadline.dll --> IceRpc.dll
    IceRpc.Extensions.DependencyInjection.dll --> di
    IceRpc.Extensions.DependencyInjection.dll --> options
    IceRpc.Extensions.DependencyInjection.dll --> IceRpc.dll
```
