| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | 4 | |
|
| | 5 | | namespace IceRpc.Extensions.DependencyInjection.Internal; |
| | 6 | |
|
| | 7 | | /// <summary>Adapts a service managed by the DI container to an IDispatcher.</summary> |
| | 8 | | internal class ServiceAdapter<TService> : IDispatcher where TService : notnull |
| | 9 | | { |
| | 10 | | public ValueTask<OutgoingResponse> DispatchAsync(IncomingRequest request, CancellationToken cancellationToken) |
| 91 | 11 | | { |
| 91 | 12 | | IServiceProviderFeature feature = request.Features.Get<IServiceProviderFeature>() ?? |
| 91 | 13 | | throw new InvalidOperationException( |
| 91 | 14 | | $"There is no {nameof(IServiceProviderFeature)} in the request features."); |
| | 15 | |
|
| 91 | 16 | | TService service = feature.ServiceProvider.GetRequiredService<TService>(); |
| | 17 | |
|
| 91 | 18 | | return ((IDispatcher)service).DispatchAsync(request, cancellationToken); |
| 91 | 19 | | } |
| | 20 | | } |