| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | namespace IceRpc.Protobuf; |
| | 4 | |
|
| | 5 | | /// <summary>Provides extension methods for <see cref="IServiceProvider" /> to create Protobuf clients.</summary> |
| | 6 | | public static class ProtobufServiceProviderExtensions |
| | 7 | | { |
| | 8 | | /// <summary>Creates a Protobuf client with this service provider.</summary> |
| | 9 | | /// <typeparam name="TClient">The Protobuf client struct.</typeparam> |
| | 10 | | /// <param name="provider">The service provider.</param> |
| | 11 | | /// <param name="serviceAddress">The service address of the new client; null is equivalent to the default service |
| | 12 | | /// address for the client type.</param> |
| | 13 | | /// <returns>A new instance of <typeparamref name="TClient" />.</returns> |
| | 14 | | /// <remarks>The new client uses the <see cref="IInvoker" /> retrieved from <paramref name="provider" /> as its |
| | 15 | | /// invocation pipeline, and the <see cref="ProtobufEncodeOptions" /> retrieved from <paramref name="provider" /> as |
| | 16 | | /// its encode options.</remarks> |
| | 17 | | public static TClient CreateProtobufClient<TClient>( |
| | 18 | | this IServiceProvider provider, |
| | 19 | | ServiceAddress? serviceAddress = null) |
| | 20 | | where TClient : struct, IProtobufClient |
| 2 | 21 | | { |
| 2 | 22 | | var invoker = (IInvoker?)provider.GetService(typeof(IInvoker)); |
| 2 | 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 | |
|
| 1 | 28 | | return serviceAddress is null ? |
| 1 | 29 | | new TClient |
| 1 | 30 | | { |
| 1 | 31 | | EncodeOptions = (ProtobufEncodeOptions?)provider.GetService(typeof(ProtobufEncodeOptions)), |
| 1 | 32 | | Invoker = invoker |
| 1 | 33 | | } |
| 1 | 34 | | : |
| 1 | 35 | | new TClient |
| 1 | 36 | | { |
| 1 | 37 | | EncodeOptions = (ProtobufEncodeOptions?)provider.GetService(typeof(ProtobufEncodeOptions)), |
| 1 | 38 | | Invoker = invoker, |
| 1 | 39 | | ServiceAddress = serviceAddress |
| 1 | 40 | | }; |
| 1 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary>Creates a Protobuf client with this service provider.</summary> |
| | 44 | | /// <typeparam name="TClient">The Protobuf client struct.</typeparam> |
| | 45 | | /// <param name="provider">The service provider.</param> |
| | 46 | | /// <param name="serviceAddressUri">The service address of the client as a URI.</param> |
| | 47 | | /// <returns>A new instance of <typeparamref name="TClient" />.</returns> |
| | 48 | | /// <remarks>The new client uses the <see cref="IInvoker" /> retrieved from <paramref name="provider" /> as its |
| | 49 | | /// invocation pipeline, and the <see cref="ProtobufEncodeOptions" /> retrieved from <paramref name="provider" /> as |
| | 50 | | /// its encode options.</remarks> |
| | 51 | | public static TClient CreateProtobufClient<TClient>(this IServiceProvider provider, Uri serviceAddressUri) |
| | 52 | | where TClient : struct, IProtobufClient => |
| 0 | 53 | | provider.CreateProtobufClient<TClient>(new ServiceAddress(serviceAddressUri)); |
| | 54 | | } |