| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Features; |
| | 4 | |
|
| | 5 | | namespace IceRpc.Extensions.DependencyInjection; |
| | 6 | |
|
| | 7 | | /// <summary>Provides extension methods for <see cref="IInvokerBuilder" /> to add the last invoker of an invocation |
| | 8 | | /// pipeline or to add a middleware that sets a feature.</summary> |
| | 9 | | public static class InvokerBuilderExtensions |
| | 10 | | { |
| | 11 | | /// <summary>Sets the last invoker of the invocation pipeline to be a DI service managed by the service provider. |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="TService">The type of the DI service.</typeparam> |
| | 14 | | /// <param name="builder">The builder being configured.</param> |
| | 15 | | /// <returns>This builder.</returns> |
| | 16 | | public static IInvokerBuilder Into<TService>(this IInvokerBuilder builder) where TService : IInvoker |
| 6 | 17 | | { |
| 6 | 18 | | object? into = builder.ServiceProvider.GetService(typeof(TService)); |
| 6 | 19 | | return into is not null ? builder.Into((IInvoker)into) : |
| 6 | 20 | | throw new InvalidOperationException( |
| 6 | 21 | | $"Could not find service of type '{typeof(TService)}' in the service container."); |
| 6 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary>Adds an interceptor that sets a feature in all requests.</summary> |
| | 25 | | /// <typeparam name="TFeature">The type of the feature.</typeparam> |
| | 26 | | /// <param name="builder">The builder being configured.</param> |
| | 27 | | /// <param name="feature">The value of the feature to set.</param> |
| | 28 | | /// <returns>The builder.</returns> |
| | 29 | | public static IInvokerBuilder UseFeature<TFeature>(this IInvokerBuilder builder, TFeature feature) => |
| 0 | 30 | | builder.Use(next => new InlineInvoker((request, cancellationToken) => |
| 0 | 31 | | { |
| 0 | 32 | | request.Features = request.Features.With(feature); |
| 0 | 33 | | return next.InvokeAsync(request, cancellationToken); |
| 0 | 34 | | })); |
| | 35 | | } |