| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Transports; |
| | | 4 | | using IceRpc.Transports.Quic; |
| | | 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 | | using System.Net.Quic; |
| | | 11 | | |
| | | 12 | | namespace IceRpc.Extensions.DependencyInjection; |
| | | 13 | | |
| | | 14 | | /// <summary>Provides an extension method for <see cref="IServiceCollection" /> to add a client connection.</summary> |
| | | 15 | | public static class ClientConnectionServiceCollectionExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary>Adds a <see cref="ClientConnection" /> and an <see cref="IInvoker" /> to this service collection. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="services">The service collection to add services to.</param> |
| | | 20 | | /// <returns>The service collection.</returns> |
| | | 21 | | /// <remarks>This method uses the client connection options provided by the <see cref="IOptions{T}" /> of |
| | | 22 | | /// <see cref="ClientConnectionOptions" />.</remarks> |
| | | 23 | | /// <example> |
| | | 24 | | /// The following code adds a ClientConnection singleton to the service collection. |
| | | 25 | | /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcClientConnectionExamp |
| | | 26 | | /// region="ClientConnectionWithOptions" lang="csharp" /> |
| | | 27 | | /// You can also inject a client transport: |
| | | 28 | | /// <list type="bullet"> |
| | | 29 | | /// <item><description>an <see cref="IDuplexClientTransport" /> for the ice protocol</description></item> |
| | | 30 | | /// <item><description>an <see cref="IMultiplexedClientTransport" /> for the icerpc protocol</description></item> |
| | | 31 | | /// </list> |
| | | 32 | | /// For example, you can add a Slic over TCP client connection as follows: |
| | | 33 | | /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcClientConnectionExamp |
| | | 34 | | /// region="ClientConnectionWithSlic" lang="csharp" /> |
| | | 35 | | /// If you want to customize the options of the default multiplexed transport (QUIC), you just need to inject an |
| | | 36 | | /// <see cref="IOptions{T}" /> of <see cref="QuicClientTransportOptions" />. |
| | | 37 | | /// </example> |
| | | 38 | | public static IServiceCollection AddIceRpcClientConnection(this IServiceCollection services) => |
| | 23 | 39 | | services |
| | 23 | 40 | | .TryAddIceRpcClientTransport() |
| | 23 | 41 | | .AddSingleton(provider => |
| | 23 | 42 | | new ClientConnection( |
| | 23 | 43 | | provider.GetRequiredService<IOptions<ClientConnectionOptions>>().Value, |
| | 23 | 44 | | provider.GetRequiredService<IDuplexClientTransport>(), |
| | 23 | 45 | | provider.GetRequiredService<IMultiplexedClientTransport>(), |
| | 23 | 46 | | provider.GetService<ILogger<ClientConnection>>())) |
| | 25 | 47 | | .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ClientConnection>()); |
| | | 48 | | |
| | | 49 | | internal static IServiceCollection TryAddIceRpcClientTransport(this IServiceCollection services) |
| | 27 | 50 | | { |
| | | 51 | | // The default duplex transport is TCP. |
| | 27 | 52 | | services |
| | 27 | 53 | | .AddOptions() |
| | 27 | 54 | | .TryAddSingleton<IDuplexClientTransport>( |
| | 27 | 55 | | provider => new TcpClientTransport( |
| | 27 | 56 | | provider.GetRequiredService<IOptions<TcpClientTransportOptions>>().Value)); |
| | | 57 | | |
| | 27 | 58 | | services |
| | 27 | 59 | | .TryAddSingleton<IMultiplexedClientTransport>( |
| | 27 | 60 | | provider => |
| | 0 | 61 | | { |
| | 0 | 62 | | if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsWindows()) |
| | 0 | 63 | | { |
| | 0 | 64 | | if (QuicConnection.IsSupported) |
| | 0 | 65 | | { |
| | 0 | 66 | | return new QuicClientTransport( |
| | 0 | 67 | | provider.GetRequiredService<IOptions<QuicClientTransportOptions>>().Value); |
| | 27 | 68 | | } |
| | 0 | 69 | | throw new NotSupportedException( |
| | 0 | 70 | | "The default QUIC client transport is not available on this system. Please review the Platfo |
| | 27 | 71 | | } |
| | 0 | 72 | | throw new PlatformNotSupportedException( |
| | 0 | 73 | | "The default QUIC client transport is not supported on this platform. You need to register an IM |
| | 27 | 74 | | }); |
| | | 75 | | |
| | 27 | 76 | | return services; |
| | 27 | 77 | | } |
| | | 78 | | } |