< 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: 2300_35243572715
Line coverage
80%
Covered lines: 28
Uncovered lines: 7
Coverable lines: 35
Total lines: 106
Line coverage: 80%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage
100%
Covered methods: 4
Fully covered methods: 3
Total methods: 4
Method coverage: 100%
Full method coverage: 75%

Metrics

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

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;
 10
 11namespace IceRpc.Extensions.DependencyInjection;
 12
 13/// <summary>Provides extension methods for <see cref="IServiceCollection" /> to add a client connection.</summary>
 14public static class ClientConnectionServiceCollectionExtensions
 15{
 16    /// <summary>Adds a <see cref="ClientConnection" /> singleton that connects to the specified server address to this
 17    /// service collection; this singleton is also registered as the <see cref="IInvoker" /> singleton.</summary>
 18    /// <param name="services">The service collection to add services to.</param>
 19    /// <param name="serverAddress">The server address of the client connection.</param>
 20    /// <returns>The service collection.</returns>
 21    /// <remarks>This method sets <see cref="ClientConnectionOptions.ServerAddress" /> in the client connection options
 22    /// provided by the <see cref="IOptions{T}" /> of <see cref="ClientConnectionOptions" />; the client connection uses
 23    /// all the other options from these injected options. If your application connects to multiple servers, call
 24    /// <see cref="ConnectionCacheServiceCollectionExtensions.AddIceRpcConnectionCache(IServiceCollection)" />
 25    /// instead.</remarks>
 26    /// <seealso cref="AddIceRpcClientConnection(IServiceCollection)" />
 27    public static IServiceCollection AddIceRpcClientConnection(
 28        this IServiceCollection services,
 29        ServerAddress serverAddress)
 130    {
 231        services.AddOptions<ClientConnectionOptions>().Configure(options => options.ServerAddress = serverAddress);
 132        return services.AddIceRpcClientConnection();
 133    }
 34
 35    /// <summary>Adds a <see cref="ClientConnection" /> singleton that connects to the specified server address URI to
 36    /// this service collection; this singleton is also registered as the <see cref="IInvoker" /> singleton.</summary>
 37    /// <param name="services">The service collection to add services to.</param>
 38    /// <param name="serverAddressUri">The server address URI of the client connection.</param>
 39    /// <returns>The service collection.</returns>
 40    /// <remarks>If your application connects to multiple servers, call
 41    /// <see cref="ConnectionCacheServiceCollectionExtensions.AddIceRpcConnectionCache(IServiceCollection)" />
 42    /// instead.</remarks>
 43    /// <seealso cref="AddIceRpcClientConnection(IServiceCollection, ServerAddress)" />
 44    public static IServiceCollection AddIceRpcClientConnection(this IServiceCollection services, Uri serverAddressUri) =
 145        services.AddIceRpcClientConnection(new ServerAddress(serverAddressUri));
 46
 47    /// <summary>Adds a <see cref="ClientConnection" /> singleton to this service collection; this singleton is also
 48    /// registered as the <see cref="IInvoker" /> singleton.</summary>
 49    /// <param name="services">The service collection to add services to.</param>
 50    /// <returns>The service collection.</returns>
 51    /// <remarks>This method uses the client connection options provided by the <see cref="IOptions{T}" /> of
 52    /// <see cref="ClientConnectionOptions" />. If your application connects to multiple servers, call
 53    /// <see cref="ConnectionCacheServiceCollectionExtensions.AddIceRpcConnectionCache(IServiceCollection)" />
 54    /// instead.</remarks>
 55    /// <example>
 56    /// The following code adds a ClientConnection singleton to the service collection.
 57    /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcClientConnectionExamp
 58    /// region="ClientConnectionWithOptions" lang="csharp" />
 59    /// You can also inject a client transport:
 60    /// <list type="bullet">
 61    /// <item><description>an <see cref="IDuplexClientTransport" /> for the ice protocol</description></item>
 62    /// <item><description>an <see cref="IMultiplexedClientTransport" /> for the icerpc protocol</description></item>
 63    /// </list>
 64    /// For example, you can add a Slic over TCP client connection as follows:
 65    /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcClientConnectionExamp
 66    /// region="ClientConnectionWithSlic" lang="csharp" />
 67    /// If you want to customize the options of the default multiplexed transport (QUIC), you just need to inject an
 68    /// <see cref="IOptions{T}" /> of <see cref="QuicClientTransportOptions" />.
 69    /// </example>
 70    public static IServiceCollection AddIceRpcClientConnection(this IServiceCollection services) =>
 2671        services
 2672            .TryAddIceRpcClientTransport()
 2673            .AddSingleton(provider =>
 2674                new ClientConnection(
 2675                    provider.GetRequiredService<IOptions<ClientConnectionOptions>>().Value,
 2676                    provider.GetRequiredService<IDuplexClientTransport>(),
 2677                    provider.GetRequiredService<IMultiplexedClientTransport>(),
 2678                    provider.GetService<ILogger<ClientConnection>>()))
 2879            .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ClientConnection>());
 80
 81    internal static IServiceCollection TryAddIceRpcClientTransport(this IServiceCollection services)
 3082    {
 83        // The default duplex transport is TCP.
 3084        services
 3085            .AddOptions()
 3086            .TryAddSingleton<IDuplexClientTransport>(
 3087                provider => new TcpClientTransport(
 3088                    provider.GetRequiredService<IOptions<TcpClientTransportOptions>>().Value));
 89
 3090        services
 3091            .TryAddSingleton<IMultiplexedClientTransport>(
 3092                provider =>
 093                {
 094                    if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsWindows())
 095                    {
 3096                        // This works even when QUIC is not available.
 097                        return new QuicClientTransport(
 098                            provider.GetRequiredService<IOptions<QuicClientTransportOptions>>().Value);
 3099                    }
 0100                    throw new PlatformNotSupportedException(
 0101                        "The default QUIC client transport is not supported on this platform. You need to register an IM
 30102                });
 103
 30104        return services;
 30105    }
 106}