Table of Contents

Class RequestContextRouterExtensions

Namespace
IceRpc
Assembly
IceRpc.RequestContext.dll

Provides an extension method for Router to add the request context middleware.

public static class RequestContextRouterExtensions
Inheritance
RequestContextRouterExtensions
Inherited Members

Methods

UseRequestContext(Router)

Adds a RequestContextMiddleware to this router.

public static Router UseRequestContext(this Router router)

Parameters

router Router

The router being configured.

Returns

Router

The router being configured.

Examples

The following code adds the request context middleware to the dispatch pipeline.

// Create a router (dispatch pipeline) and install the request context middleware.
Router router = new Router()
    .UseRequestContext()
    .Map<IGreeterService>(new Chatbot());

await using var server = new Server(router);
server.Listen();

The greeter service can then access the request context from the features parameter as shown in the following code.

public ValueTask<string> GreetAsync(
    string name,
    IFeatureCollection features,
    CancellationToken cancellationToken)
{
    if (features.Get<IRequestContextFeature>() is IRequestContextFeature contextFeature)
    {
        foreach ((string key, string value) in contextFeature.Value)
        {
            // ...
        }
    }
    return new($"Hello, {name}!");
}
See Also