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