| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Transports; |
| | 4 | | using IceRpc.Transports.Slic; |
| | 5 | | using IceRpc.Transports.Tcp; |
| | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | 7 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | 8 | | using Microsoft.Extensions.Logging; |
| | 9 | | using Microsoft.Extensions.Options; |
| | 10 | |
|
| | 11 | | namespace IceRpc.Extensions.DependencyInjection; |
| | 12 | |
|
| | 13 | | /// <summary>Provides an extension method for <see cref="IServiceCollection" /> to add a client connection.</summary> |
| | 14 | | public static class ClientConnectionServiceCollectionExtensions |
| | 15 | | { |
| | 16 | | /// <summary>Adds a <see cref="ClientConnection" /> and an <see cref="IInvoker" /> to this service collection. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="services">The service collection to add services to.</param> |
| | 19 | | /// <returns>The service collection.</returns> |
| | 20 | | /// <remarks>This method uses the client connection options provided by the <see cref="IOptions{T}" /> of |
| | 21 | | /// <see cref="ClientConnectionOptions" />.</remarks> |
| | 22 | | /// <example> |
| | 23 | | /// The following code adds a ClientConnection singleton to the service collection. |
| | 24 | | /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcClientConnectionExamp |
| | 25 | | /// region="ClientConnectionWithOptions" lang="csharp" /> |
| | 26 | | /// You can also inject a client transport: |
| | 27 | | /// <list type="bullet"> |
| | 28 | | /// <item><description>an <see cref="IDuplexClientTransport" /> for the ice protocol</description></item> |
| | 29 | | /// <item><description>an <see cref="IMultiplexedClientTransport" /> for the icerpc protocol</description></item> |
| | 30 | | /// </list> |
| | 31 | | /// For example, you can add a QUIC client connection as follows: |
| | 32 | | /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcClientConnectionExamp |
| | 33 | | /// region="ClientConnectionWithQuic" lang="csharp" /> |
| | 34 | | /// If you want to customize the options of the default transport (tcp), you just need to inject an |
| | 35 | | /// <see cref="IOptions{T}" /> of <see cref="TcpClientTransportOptions" />. |
| | 36 | | /// </example> |
| | 37 | | public static IServiceCollection AddIceRpcClientConnection(this IServiceCollection services) => |
| 40 | 38 | | services |
| 40 | 39 | | .TryAddIceRpcClientTransport() |
| 40 | 40 | | .AddSingleton(provider => |
| 40 | 41 | | new ClientConnection( |
| 40 | 42 | | provider.GetRequiredService<IOptions<ClientConnectionOptions>>().Value, |
| 40 | 43 | | provider.GetRequiredService<IDuplexClientTransport>(), |
| 40 | 44 | | provider.GetRequiredService<IMultiplexedClientTransport>(), |
| 40 | 45 | | provider.GetService<ILogger<ClientConnection>>())) |
| 42 | 46 | | .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ClientConnection>()); |
| | 47 | |
|
| | 48 | | internal static IServiceCollection TryAddIceRpcClientTransport(this IServiceCollection services) |
| 44 | 49 | | { |
| 44 | 50 | | services |
| 44 | 51 | | .AddOptions() |
| 44 | 52 | | .TryAddSingleton<IDuplexClientTransport>( |
| 44 | 53 | | provider => new TcpClientTransport( |
| 44 | 54 | | provider.GetRequiredService<IOptions<TcpClientTransportOptions>>().Value)); |
| | 55 | |
|
| 44 | 56 | | services. |
| 44 | 57 | | TryAddSingleton<IMultiplexedClientTransport>( |
| 88 | 58 | | provider => new SlicClientTransport( |
| 88 | 59 | | provider.GetRequiredService<IOptions<SlicTransportOptions>>().Value, |
| 88 | 60 | | provider.GetRequiredService<IDuplexClientTransport>())); |
| | 61 | |
|
| 44 | 62 | | return services; |
| 44 | 63 | | } |
| | 64 | | } |