• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

icerpc / icerpc-csharp / 20870171951

10 Jan 2026 01:03AM UTC coverage: 83.304% (-0.1%) from 83.399%
20870171951

Pull #4221

github

web-flow
Merge fe4f556d8 into f39e9819d
Pull Request #4221: Use new C#14 extension block syntax

12030 of 14441 relevant lines covered (83.3%)

2957.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.11
src/IceRpc.Extensions.DependencyInjection/ClientConnectionServiceCollectionExtensions.cs
1
// Copyright (c) ZeroC, Inc.
2

3
using IceRpc.Transports;
4
using IceRpc.Transports.Quic;
5
using IceRpc.Transports.Tcp;
6
using Microsoft.Extensions.DependencyInjection;
7
using Microsoft.Extensions.DependencyInjection.Extensions;
8
using Microsoft.Extensions.Logging;
9
using Microsoft.Extensions.Options;
10
using System.Net.Quic;
11

12
namespace IceRpc.Extensions.DependencyInjection;
13

14
/// <summary>Provides an extension method for <see cref="IServiceCollection" /> to add a client connection.</summary>
15
public static class ClientConnectionServiceCollectionExtensions
16
{
17
    /// <summary>Extension methods for <see cref="IServiceCollection" />.</summary>
18
    /// <param name="services">The service collection to add services to.</param>
19
    extension(IServiceCollection services)
20
    {
21
        /// <summary>Adds a <see cref="ClientConnection" /> and an <see cref="IInvoker" /> to this service collection.
22
        /// </summary>
23
        /// <returns>The service collection.</returns>
24
        /// <remarks>This method uses the client connection options provided by the <see cref="IOptions{T}" /> of
25
        /// <see cref="ClientConnectionOptions" />.</remarks>
26
        /// <example>
27
        /// The following code adds a ClientConnection singleton to the service collection.
28
        /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/
29
        ///     AddIceRpcClientConnectionExamples.cs"
30
        ///     region="ClientConnectionWithOptions"
31
        ///     lang="csharp" />
32
        /// You can also inject a client transport:
33
        /// <list type="bullet">
34
        /// <item><description>an <see cref="IDuplexClientTransport" /> for the ice protocol</description>
35
        /// </item>
36
        /// <item><description>an <see cref="IMultiplexedClientTransport" /> for the icerpc protocol
37
        /// </description></item>
38
        /// </list>
39
        /// For example, you can add a Slic over TCP client connection as follows:
40
        /// <code source="../../docfx/examples/IceRpc.Extensions.DependencyInjection.Examples/
41
        ///     AddIceRpcClientConnectionExamples.cs"
42
        ///     region="ClientConnectionWithSlic"
43
        ///     lang="csharp" />
44
        /// If you want to customize the options of the default multiplexed transport (QUIC), you just need to inject an
45
        /// <see cref="IOptions{T}" /> of <see cref="QuicClientTransportOptions" />.
46
        /// </example>
47
        public IServiceCollection AddIceRpcClientConnection() =>
48
            services
23✔
49
                .TryAddIceRpcClientTransport()
23✔
50
                .AddSingleton(provider =>
23✔
51
                    new ClientConnection(
23✔
52
                        provider.GetRequiredService<IOptions<ClientConnectionOptions>>().Value,
23✔
53
                        provider.GetRequiredService<IDuplexClientTransport>(),
23✔
54
                        provider.GetRequiredService<IMultiplexedClientTransport>(),
23✔
55
                        provider.GetService<ILogger<ClientConnection>>()))
23✔
56
                .AddSingleton<IInvoker>(provider => provider.GetRequiredService<ClientConnection>());
25✔
57

58
        internal IServiceCollection TryAddIceRpcClientTransport()
59
        {
27✔
60
            // The default duplex transport is TCP.
61
            services
27✔
62
                .AddOptions()
27✔
63
                .TryAddSingleton<IDuplexClientTransport>(
27✔
64
                    provider => new TcpClientTransport(
27✔
65
                        provider.GetRequiredService<IOptions<TcpClientTransportOptions>>().Value));
27✔
66

67
            services
27✔
68
                .TryAddSingleton<IMultiplexedClientTransport>(
27✔
69
                    provider =>
27✔
70
                    {
×
71
                        if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsWindows())
×
72
                        {
×
73
                            if (QuicConnection.IsSupported)
×
74
                            {
×
75
                                return new QuicClientTransport(
×
76
                                    provider.GetRequiredService<IOptions<QuicClientTransportOptions>>().Value);
×
77
                            }
27✔
78
                            throw new NotSupportedException(
×
79
                                "The default QUIC client transport is not available on this system. " +
×
80
                                "Please review the Platform Dependencies for QUIC in the .NET documentation.");
×
81
                        }
27✔
82
                        throw new PlatformNotSupportedException(
×
83
                            "The default QUIC client transport is not supported on this platform. " +
×
84
                            "You need to register an IMultiplexedClientTransport implementation " +
×
85
                            "in the service collection.");
×
86
                    });
27✔
87

88
            return services;
27✔
89
        }
90
    }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc