---
title: "Middleware"
description: "Understand how to install and write a middleware."
url: https://docs.icerpc.dev/icerpc/dispatch/middleware
docs_index: https://docs.icerpc.dev/llms.txt
---

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

# Middleware

Understand how to install and write a middleware.

## Intercepting incoming requests

A middleware is a piece of code that intercepts an incoming request before this request reaches the target service. The same code also intercepts the outgoing response provided by the service before it's sent back to the caller.

At a more technical level, a middleware is a [dispatcher](https://docs.icerpc.dev/icerpc/dispatch/dispatch-pipeline.md#the-dispatcher-abstraction) that holds another dispatcher ("next") and calls `dispatch` on this next dispatcher as part of the implementation of its own `dispatch` method. The next dispatcher can be another middleware, a service, a router, or some other kind of dispatcher; as far as the middleware is concerned, it's just another dispatcher.

A middleware can include logic before calling `dispatch` on the next dispatcher (before the request is processed) and after calling `dispatch` on the next dispatcher (after it receives the response). A middleware can also short-circuit the dispatch pipeline by returning a cached response or by returning an error (a response with a status code other than `Ok`).

For example, a simple C# middleware could look like:

```csharp
internal class SimpleMiddleware : IDispatcher
{
    private readonly IDispatcher _next;

    public async ValueTask<OutgoingResponse> DispatchAsync(
        IncomingRequest request,
        CancellationToken cancellationToken)
    {
        Console.WriteLine("before _next.DispatchAsync");
        OutgoingResponse response = await _next.DispatchAsync(request, cancellationToken);
        Console.WriteLine($"after _next.DispatchAsync; the response status code is {response.StatusCode}");
        return response;
    }

    internal SimpleMiddleware(IDispatcher next) => _next = next;
}
```

## Installing a middleware

You can use a [Router](https://docs.icerpc.dev/icerpc/dispatch/router.md) to create a dispatch pipeline and install one or more middleware in this dispatch pipeline.
