< Summary

Information
Class: IceRpc.Protobuf.ProtobufServiceProviderExtensions
Assembly: IceRpc.Protobuf
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Protobuf/ProtobufServiceProviderExtensions.cs
Tag: 275_13775359185
Line coverage
95%
Covered lines: 19
Uncovered lines: 1
Coverable lines: 20
Total lines: 54
Line coverage: 95%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage
50%
Covered methods: 1
Total methods: 2
Method coverage: 50%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CreateProtobufClient(...)75%44100%
CreateProtobufClient(...)100%210%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Protobuf/ProtobufServiceProviderExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3namespace IceRpc.Protobuf;
 4
 5/// <summary>Provides extension methods for <see cref="IServiceProvider" /> to create Protobuf clients.</summary>
 6public static class ProtobufServiceProviderExtensions
 7{
 8    /// <summary>Creates a Protobuf client with this service provider.</summary>
 9    /// <typeparam name="TClient">The Protobuf client struct.</typeparam>
 10    /// <param name="provider">The service provider.</param>
 11    /// <param name="serviceAddress">The service address of the new client; null is equivalent to the default service
 12    /// address for the client type.</param>
 13    /// <returns>A new instance of <typeparamref name="TClient" />.</returns>
 14    /// <remarks>The new client uses the <see cref="IInvoker" /> retrieved from <paramref name="provider" /> as its
 15    /// invocation pipeline, and the <see cref="ProtobufEncodeOptions" /> retrieved from <paramref name="provider" /> as
 16    /// its encode options.</remarks>
 17    public static TClient CreateProtobufClient<TClient>(
 18        this IServiceProvider provider,
 19        ServiceAddress? serviceAddress = null)
 20        where TClient : struct, IProtobufClient
 221    {
 222        var invoker = (IInvoker?)provider.GetService(typeof(IInvoker));
 223        if (invoker is null)
 124        {
 125            throw new InvalidOperationException("Could not find service of type 'IInvoker' in the service container.");
 26        }
 27
 128        return serviceAddress is null ?
 129            new TClient
 130            {
 131                EncodeOptions = (ProtobufEncodeOptions?)provider.GetService(typeof(ProtobufEncodeOptions)),
 132                Invoker = invoker
 133            }
 134            :
 135            new TClient
 136            {
 137                EncodeOptions = (ProtobufEncodeOptions?)provider.GetService(typeof(ProtobufEncodeOptions)),
 138                Invoker = invoker,
 139                ServiceAddress = serviceAddress
 140            };
 141    }
 42
 43    /// <summary>Creates a Protobuf client with this service provider.</summary>
 44    /// <typeparam name="TClient">The Protobuf client struct.</typeparam>
 45    /// <param name="provider">The service provider.</param>
 46    /// <param name="serviceAddressUri">The service address of the client as a URI.</param>
 47    /// <returns>A new instance of <typeparamref name="TClient" />.</returns>
 48    /// <remarks>The new client uses the <see cref="IInvoker" /> retrieved from <paramref name="provider" /> as its
 49    /// invocation pipeline, and the <see cref="ProtobufEncodeOptions" /> retrieved from <paramref name="provider" /> as
 50    /// its encode options.</remarks>
 51    public static TClient CreateProtobufClient<TClient>(this IServiceProvider provider, Uri serviceAddressUri)
 52        where TClient : struct, IProtobufClient =>
 053        provider.CreateProtobufClient<TClient>(new ServiceAddress(serviceAddressUri));
 54}