< Summary

Information
Class: IceRpc.Extensions.DependencyInjection.ProtobufServiceProviderExtensions
Assembly: IceRpc.Protobuf
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Protobuf/DependencyInjection/ProtobufServiceProviderExtensions.cs
Tag: 1321_24790053727
Line coverage
95%
Covered lines: 19
Uncovered lines: 1
Coverable lines: 20
Total lines: 56
Line coverage: 95%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage
50%
Covered methods: 1
Fully covered methods: 1
Total methods: 2
Method coverage: 50%
Full 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/DependencyInjection/ProtobufServiceProviderExtensions.cs

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