| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Slice; |
| | | 4 | | |
| | | 5 | | namespace IceRpc.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | /// <summary>Provides extension methods for <see cref="IServiceProvider" /> to create Slice proxies.</summary> |
| | | 8 | | public static class SliceServiceProviderExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary>Creates a Slice proxy with this service provider.</summary> |
| | | 11 | | /// <typeparam name="TProxy">The Slice proxy struct.</typeparam> |
| | | 12 | | /// <param name="provider">The service provider.</param> |
| | | 13 | | /// <param name="serviceAddress">The service address of the new proxy; null is equivalent to the default service |
| | | 14 | | /// address for the proxy type.</param> |
| | | 15 | | /// <returns>A new instance of <typeparamref name="TProxy" />.</returns> |
| | | 16 | | /// <remarks>The new proxy uses the <see cref="IInvoker" /> retrieved from <paramref name="provider" /> as its |
| | | 17 | | /// invocation pipeline, and the <see cref="SliceEncodeOptions" /> retrieved from <paramref name="provider" /> as |
| | | 18 | | /// its encode options.</remarks> |
| | | 19 | | public static TProxy CreateSliceProxy<TProxy>(this IServiceProvider provider, ServiceAddress? serviceAddress = null) |
| | | 20 | | where TProxy : struct, ISliceProxy |
| | 4 | 21 | | { |
| | 4 | 22 | | var invoker = (IInvoker?)provider.GetService(typeof(IInvoker)); |
| | 4 | 23 | | if (invoker is null) |
| | 1 | 24 | | { |
| | 1 | 25 | | throw new InvalidOperationException("Could not find service of type 'IInvoker' in the service container."); |
| | | 26 | | } |
| | | 27 | | |
| | 3 | 28 | | return serviceAddress is null ? |
| | 3 | 29 | | new TProxy |
| | 3 | 30 | | { |
| | 3 | 31 | | EncodeOptions = (SliceEncodeOptions?)provider.GetService(typeof(SliceEncodeOptions)), |
| | 3 | 32 | | Invoker = invoker |
| | 3 | 33 | | } |
| | 3 | 34 | | : |
| | 3 | 35 | | new TProxy |
| | 3 | 36 | | { |
| | 3 | 37 | | EncodeOptions = (SliceEncodeOptions?)provider.GetService(typeof(SliceEncodeOptions)), |
| | 3 | 38 | | Invoker = invoker, |
| | 3 | 39 | | ServiceAddress = serviceAddress |
| | 3 | 40 | | }; |
| | 3 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary>Creates a Slice proxy with this service provider.</summary> |
| | | 44 | | /// <typeparam name="TProxy">The Slice proxy struct.</typeparam> |
| | | 45 | | /// <param name="provider">The service provider.</param> |
| | | 46 | | /// <param name="serviceAddressUri">The service address of the proxy as a URI.</param> |
| | | 47 | | /// <returns>A new instance of <typeparamref name="TProxy" />.</returns> |
| | | 48 | | /// <remarks>The new proxy uses the <see cref="IInvoker" /> retrieved from <paramref name="provider" /> as its |
| | | 49 | | /// invocation pipeline, and the <see cref="SliceEncodeOptions" /> retrieved from <paramref name="provider" /> as |
| | | 50 | | /// its encode options.</remarks> |
| | | 51 | | public static TProxy CreateSliceProxy<TProxy>(this IServiceProvider provider, Uri serviceAddressUri) |
| | | 52 | | where TProxy : struct, ISliceProxy => |
| | 2 | 53 | | provider.CreateSliceProxy<TProxy>(new ServiceAddress(serviceAddressUri)); |
| | | 54 | | } |