< 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: 701_22528036593
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
Total methods: 2
Method coverage: 100%

Metrics

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

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) =>
 2339        services
 2340            .TryAddIceRpcClientTransport()
 2341            .AddSingleton(provider =>
 2342                new ClientConnection(
 2343                    provider.GetRequiredService<IOptions<ClientConnectionOptions>>().Value,
 2344                    provider.GetRequiredService<IDuplexClientTransport>(),
 2345                    provider.GetRequiredService<IMultiplexedClientTransport>(),
 2346                    provider.GetService<ILogger<ClientConnection>>()))
 2547            .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ClientConnection>());
 48
 49    internal static IServiceCollection TryAddIceRpcClientTransport(this IServiceCollection services)
 2750    {
 51        // The default duplex transport is TCP.
 2752        services
 2753            .AddOptions()
 2754            .TryAddSingleton<IDuplexClientTransport>(
 2755                provider => new TcpClientTransport(
 2756                    provider.GetRequiredService<IOptions<TcpClientTransportOptions>>().Value));
 57
 2758        services
 2759            .TryAddSingleton<IMultiplexedClientTransport>(
 2760                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);
 2768                        }
 069                        throw new NotSupportedException(
 070                            "The default QUIC client transport is not available on this system. Please review the Platfo
 2771                    }
 072                    throw new PlatformNotSupportedException(
 073                        "The default QUIC client transport is not supported on this platform. You need to register an IM
 2774                });
 75
 2776        return services;
 2777    }
 78}