< Summary

Information
Class: IceRpc.Extensions.DependencyInjection.ClientConnectionServiceCollectionExtensions
Assembly: IceRpc.Extensions.DependencyInjection
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Extensions.DependencyInjection/ClientConnectionServiceCollectionExtensions.cs
Tag: 1856_27024993493
Line coverage
67%
Covered lines: 23
Uncovered lines: 11
Coverable lines: 34
Total lines: 78
Line coverage: 67.6%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage
100%
Covered methods: 2
Fully covered methods: 1
Total methods: 2
Method coverage: 100%
Full method coverage: 50%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddIceRpcClientConnection(...)100%11100%
TryAddIceRpcClientTransport(...)0%13856%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Extensions.DependencyInjection/ClientConnectionServiceCollectionExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Transports;
 4using IceRpc.Transports.Quic;
 5using IceRpc.Transports.Tcp;
 6using Microsoft.Extensions.DependencyInjection;
 7using Microsoft.Extensions.DependencyInjection.Extensions;
 8using Microsoft.Extensions.Logging;
 9using Microsoft.Extensions.Options;
 10using System.Net.Quic;
 11
 12namespace IceRpc.Extensions.DependencyInjection;
 13
 14/// <summary>Provides an extension method for <see cref="IServiceCollection" /> to add a client connection.</summary>
 15public 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) =>
 2539        services
 2540            .TryAddIceRpcClientTransport()
 2541            .AddSingleton(provider =>
 2542                new ClientConnection(
 2543                    provider.GetRequiredService<IOptions<ClientConnectionOptions>>().Value,
 2544                    provider.GetRequiredService<IDuplexClientTransport>(),
 2545                    provider.GetRequiredService<IMultiplexedClientTransport>(),
 2546                    provider.GetService<ILogger<ClientConnection>>()))
 2747            .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ClientConnection>());
 48
 49    internal static IServiceCollection TryAddIceRpcClientTransport(this IServiceCollection services)
 2950    {
 51        // The default duplex transport is TCP.
 2952        services
 2953            .AddOptions()
 2954            .TryAddSingleton<IDuplexClientTransport>(
 2955                provider => new TcpClientTransport(
 2956                    provider.GetRequiredService<IOptions<TcpClientTransportOptions>>().Value));
 57
 2958        services
 2959            .TryAddSingleton<IMultiplexedClientTransport>(
 2960                provider =>
 061                {
 062                    if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsWindows())
 063                    {
 064                        if (QuicConnection.IsSupported)
 065                        {
 066                            return new QuicClientTransport(
 067                                provider.GetRequiredService<IOptions<QuicClientTransportOptions>>().Value);
 2968                        }
 069                        throw new NotSupportedException(
 070                            "The default QUIC client transport is not available on this system. Please review the Platfo
 2971                    }
 072                    throw new PlatformNotSupportedException(
 073                        "The default QUIC client transport is not supported on this platform. You need to register an IM
 2974                });
 75
 2976        return services;
 2977    }
 78}