< 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: 275_13775359185
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 64
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
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(...)100%11100%

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.Slic;
 5using IceRpc.Transports.Tcp;
 6using Microsoft.Extensions.DependencyInjection;
 7using Microsoft.Extensions.DependencyInjection.Extensions;
 8using Microsoft.Extensions.Logging;
 9using Microsoft.Extensions.Options;
 10
 11namespace IceRpc.Extensions.DependencyInjection;
 12
 13/// <summary>Provides an extension method for <see cref="IServiceCollection" /> to add a client connection.</summary>
 14public 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) =>
 4038        services
 4039            .TryAddIceRpcClientTransport()
 4040            .AddSingleton(provider =>
 4041                new ClientConnection(
 4042                    provider.GetRequiredService<IOptions<ClientConnectionOptions>>().Value,
 4043                    provider.GetRequiredService<IDuplexClientTransport>(),
 4044                    provider.GetRequiredService<IMultiplexedClientTransport>(),
 4045                    provider.GetService<ILogger<ClientConnection>>()))
 4246            .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ClientConnection>());
 47
 48    internal static IServiceCollection TryAddIceRpcClientTransport(this IServiceCollection services)
 4449    {
 4450        services
 4451            .AddOptions()
 4452            .TryAddSingleton<IDuplexClientTransport>(
 4453                provider => new TcpClientTransport(
 4454                    provider.GetRequiredService<IOptions<TcpClientTransportOptions>>().Value));
 55
 4456        services.
 4457            TryAddSingleton<IMultiplexedClientTransport>(
 8858                provider => new SlicClientTransport(
 8859                    provider.GetRequiredService<IOptions<SlicTransportOptions>>().Value,
 8860                    provider.GetRequiredService<IDuplexClientTransport>()));
 61
 4462        return services;
 4463    }
 64}