< Summary

Information
Class: IceRpc.Extensions.DependencyInjection.ConnectionCacheServiceCollectionExtensions
Assembly: IceRpc.Extensions.DependencyInjection
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Extensions.DependencyInjection/ConnectionCacheServiceCollectionExtensions.cs
Tag: 275_13775359185
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 51
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 1
Total methods: 1
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddIceRpcConnectionCache(...)100%11100%

File(s)

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

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Transports;
 4using IceRpc.Transports.Tcp;
 5using Microsoft.Extensions.DependencyInjection;
 6using Microsoft.Extensions.Logging;
 7using Microsoft.Extensions.Options;
 8
 9namespace IceRpc.Extensions.DependencyInjection;
 10
 11/// <summary>Provides an extension method for <see cref="IServiceCollection" /> to add a connection cache.</summary>
 12public static class ConnectionCacheServiceCollectionExtensions
 13{
 14    /// <summary>Adds a <see cref="ConnectionCache" /> and an <see cref="IInvoker" /> to this service collection.
 15    /// </summary>
 16    /// <param name="services">The service collection to add services to.</param>
 17    /// <returns>The service collection.</returns>
 18    /// <remarks>This method uses the connection cache options provided by the <see cref="IOptions{T}" /> of
 19    /// <see cref="ConnectionCacheOptions" />.</remarks>
 20    /// <example>
 21    /// The following code adds a ConnectionCache singleton to the service collection.
 22    /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcConnectionCacheExampl
 23    /// region="DefaultConnectionCache" lang="csharp" />
 24    /// The resulting singleton is a default connection cache. If you want to customize this connection cache, add an
 25    /// <see cref="IOptions{T}" /> of <see cref="ConnectionCacheOptions" /> to your DI container:
 26    /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcConnectionCacheExampl
 27    /// region="ConnectionCacheWithOptions" lang="csharp" />
 28    /// You can also inject a client transport:
 29    /// <list type="bullet">
 30    /// <item><description>an <see cref="IDuplexClientTransport" /> for the ice protocol</description></item>
 31    /// <item><description>an <see cref="IMultiplexedClientTransport" /> for the icerpc protocol</description></item>
 32    /// </list>
 33    /// The following example shows a connection cache that uses QUIC for icerpc connections and keeps the default
 34    /// duplex transport (tcp) for ice connections.
 35    /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/AddIceRpcConnectionCacheExampl
 36    /// region="ConnectionCacheWithQuic"
 37    /// lang="csharp" />
 38    /// If you want to customize the options of the default transport (tcp), you just need to inject an
 39    /// <see cref="IOptions{T}" /> of <see cref="TcpClientTransportOptions" />.
 40    /// </example>
 41    public static IServiceCollection AddIceRpcConnectionCache(this IServiceCollection services) =>
 442        services
 443            .TryAddIceRpcClientTransport()
 444            .AddSingleton(provider =>
 445                new ConnectionCache(
 446                    provider.GetRequiredService<IOptions<ConnectionCacheOptions>>().Value,
 447                    provider.GetRequiredService<IDuplexClientTransport>(),
 448                    provider.GetRequiredService<IMultiplexedClientTransport>(),
 449                    provider.GetService<ILogger<ConnectionCache>>()))
 450            .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ConnectionCache>());
 51}