< Summary

Class:Microsoft.Extensions.Azure.ClientRegistration`2
Assembly:Microsoft.Extensions.Azure
File(s):C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\ClientRegistration{TClient,TOptions}.cs
Covered lines:23
Uncovered lines:1
Coverable lines:24
Total lines:68
Line coverage:95.8% (23 of 24)
Covered branches:10
Total branches:12
Branch coverage:83.3% (10 of 12)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Name()-100%100%
get_Version()-100%100%
get_RequiresTokenCredential()-100%100%
.ctor(...)-100%100%
GetClient(...)-93.75%83.33%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\ClientRegistration{TClient,TOptions}.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Runtime.ExceptionServices;
 6using Azure.Core;
 7
 8namespace Microsoft.Extensions.Azure
 9{
 10    internal class ClientRegistration<TClient, TOptions>
 11    {
 17212        public string Name { get; set; }
 4813        public object Version { get; set; }
 9614        public bool RequiresTokenCredential { get; set; }
 15
 16        private readonly Func<TOptions, TokenCredential, TClient> _factory;
 17
 5018        private readonly object _cacheLock = new object();
 19
 20        private TClient _cachedClient;
 21
 22        private ExceptionDispatchInfo _cachedException;
 23
 5024        public ClientRegistration(string name, Func<TOptions, TokenCredential, TClient> factory)
 25        {
 5026            Name = name;
 5027            _factory = factory;
 5028        }
 29
 30        public TClient GetClient(TOptions options, TokenCredential tokenCredential)
 31        {
 5032            _cachedException?.Throw();
 33
 4834            if (_cachedClient != null)
 35            {
 236                return _cachedClient;
 37            }
 38
 4639            lock (_cacheLock)
 40            {
 4641                _cachedException?.Throw();
 42
 4643                if (_cachedClient != null)
 44                {
 045                    return _cachedClient;
 46                }
 47
 48
 4649                if (RequiresTokenCredential && tokenCredential == null)
 50                {
 251                    throw new InvalidOperationException("Client registration requires a TokenCredential. Configure it us
 52                }
 53
 54                try
 55                {
 4456                    _cachedClient = _factory(options, tokenCredential);
 4257                }
 258                catch (Exception e)
 59                {
 260                    _cachedException = ExceptionDispatchInfo.Capture(e);
 261                    throw;
 62                }
 63
 4264                return _cachedClient;
 65            }
 4266        }
 67    }
 68}