| | 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="IDispatcherBuilder" /> to add a middleware that sets a feature. |
| | 8 | | /// </summary> |
| | 9 | | public static class DispatcherBuilderExtensions |
| | 10 | | { |
| | 11 | | /// <summary>Registers a route to a service that uses the service's default path as the route path. If there is an |
| | 12 | | /// existing route at the same path, it is replaced.</summary> |
| | 13 | | /// <typeparam name="TService">The type of the DI service that will handle the requests. The implementation of this |
| | 14 | | /// service must implement <see cref="IDispatcher" />.</typeparam> |
| | 15 | | /// <param name="builder">The builder being configured.</param> |
| | 16 | | /// <returns>This builder.</returns> |
| | 17 | | /// <remarks>This default path is specific to services that implement Slice interfaces and |
| | 18 | | /// <typeparamref name="TService" /> must correspond to an I{Name}Service interface generated by the Slice compiler. |
| | 19 | | /// </remarks> |
| | 20 | | public static IDispatcherBuilder Map<TService>(this IDispatcherBuilder builder) where TService : notnull => |
| 0 | 21 | | builder.Map<TService>(typeof(TService).GetDefaultServicePath()); |
| | 22 | |
|
| | 23 | | /// <summary>Adds a middleware that creates and inserts the <see cref="IDispatchInformationFeature" /> feature |
| | 24 | | /// in all requests.</summary> |
| | 25 | | /// <param name="builder">The builder being configured.</param> |
| | 26 | | /// <returns>The builder.</returns> |
| | 27 | | public static IDispatcherBuilder UseDispatchInformation(this IDispatcherBuilder builder) => |
| 8 | 28 | | builder.Use(next => new InlineDispatcher((request, cancellationToken) => |
| 56 | 29 | | { |
| 56 | 30 | | request.Features = request.Features.With<IDispatchInformationFeature>( |
| 56 | 31 | | new DispatchInformationFeature(request)); |
| 56 | 32 | | return next.DispatchAsync(request, cancellationToken); |
| 64 | 33 | | })); |
| | 34 | |
|
| | 35 | | /// <summary>Adds a middleware that sets a feature in all requests.</summary> |
| | 36 | | /// <typeparam name="TFeature">The type of the feature.</typeparam> |
| | 37 | | /// <param name="builder">The builder being configured.</param> |
| | 38 | | /// <param name="feature">The value of the feature to set in all requests.</param> |
| | 39 | | /// <returns>The builder.</returns> |
| | 40 | | public static IDispatcherBuilder UseFeature<TFeature>(this IDispatcherBuilder builder, TFeature feature) => |
| 0 | 41 | | builder.Use(next => new InlineDispatcher((request, cancellationToken) => |
| 0 | 42 | | { |
| 0 | 43 | | request.Features = request.Features.With(feature); |
| 0 | 44 | | return next.DispatchAsync(request, cancellationToken); |
| 0 | 45 | | })); |
| | 46 | | } |