< Summary

Class:Microsoft.Extensions.Azure.AzureClientFactoryBuilder
Assembly:Microsoft.Extensions.Azure
File(s):C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\AzureClientFactoryBuilder.cs
Covered lines:37
Uncovered lines:0
Coverable lines:37
Total lines:132
Line coverage:100% (37 of 37)
Covered branches:6
Total branches:6
Branch coverage:100% (6 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
Azure.Core.Extensions.IAzureClientFactoryBuilder.RegisterClientFactory(...)-100%100%
Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration<Microsoft.Extensions.Configuration.IConfiguration>.RegisterClientFactory(...)-100%100%
ConfigureDefaults(...)-100%100%
ConfigureDefaults(...)-100%100%
ConfigureDefaults(...)-100%100%
Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential.RegisterClientFactory(...)-100%100%
UseCredential(...)-100%100%
UseCredential(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\AzureClientFactoryBuilder.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using Azure.Core;
 5using Azure.Core.Extensions;
 6using Microsoft.Extensions.Configuration;
 7using Microsoft.Extensions.DependencyInjection;
 8using Microsoft.Extensions.DependencyInjection.Extensions;
 9using Microsoft.Extensions.Options;
 10using System;
 11
 12namespace Microsoft.Extensions.Azure
 13{
 14    /// <summary>
 15    /// The builder type for registering Azure SDK clients.
 16    /// </summary>
 17    public sealed class AzureClientFactoryBuilder : IAzureClientFactoryBuilderWithConfiguration<IConfiguration>, IAzureC
 18    {
 19        private readonly IServiceCollection _serviceCollection;
 20
 21        internal const string DefaultClientName = "Default";
 22
 4423        internal AzureClientFactoryBuilder(IServiceCollection serviceCollection)
 24        {
 4425            _serviceCollection = serviceCollection;
 4426            _serviceCollection.AddOptions();
 4427            _serviceCollection.TryAddSingleton<EventSourceLogForwarder>();
 4428        }
 29
 30        IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilder.RegisterClientFactory<TClient, TOptions>(Func<
 31        {
 6232            return ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>((options, _
 33        }
 34
 35        IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithConfiguration<IConfiguration>.RegisterClien
 36        {
 637            var credentialsFromConfig = ClientFactory.CreateCredential(configuration);
 638            var clientBuilder =((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>
 1239                (options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options
 640                .ConfigureOptions(configuration);
 41
 642            if (credentialsFromConfig != null)
 43            {
 244                clientBuilder.WithCredential(credentialsFromConfig);
 45            }
 46
 647            return clientBuilder;
 48        }
 49
 50        /// <summary>
 51        /// Adds a configuration delegate that gets executed for all clients.
 52        /// </summary>
 53        /// <param name="configureOptions">The configuration delegate.</param>
 54        /// <returns>This instance.</returns>
 55        public AzureClientFactoryBuilder ConfigureDefaults(Action<ClientOptions> configureOptions)
 56        {
 1257            ConfigureDefaults((options, provider) => configureOptions(options));
 658            return this;
 59        }
 60
 61        /// <summary>
 62        /// Adds a configuration delegate that gets executed for all clients.
 63        /// </summary>
 64        /// <param name="configureOptions">The configuration delegate.</param>
 65        /// <returns>This instance.</returns>
 66        public AzureClientFactoryBuilder ConfigureDefaults(Action<ClientOptions, IServiceProvider> configureOptions)
 67        {
 1268            _serviceCollection.Configure<AzureClientsGlobalOptions>(options => options.ConfigureOptionDelegates.Add(conf
 69
 670            return this;
 71        }
 72
 73        /// <summary>
 74        /// Adds a configuration instance to initialize all clients from.
 75        /// </summary>
 76        /// <param name="configuration">The configuration instance.</param>
 77        /// <returns>This instance.</returns>
 78        public AzureClientFactoryBuilder ConfigureDefaults(IConfiguration configuration)
 79        {
 880            ConfigureDefaults(options => configuration.Bind(options));
 81
 482            var credentialsFromConfig = ClientFactory.CreateCredential(configuration);
 83
 484            if (credentialsFromConfig != null)
 85            {
 286                UseCredential(credentialsFromConfig);
 87            }
 88
 489            return this;
 90        }
 91
 92        IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithCredential.RegisterClientFactory<TClient, T
 93        {
 5094            var clientRegistration = new ClientRegistration<TClient, TOptions>(DefaultClientName, clientFactory);
 5095            clientRegistration.RequiresTokenCredential = requiresCredential;
 96
 5097            _serviceCollection.AddSingleton(clientRegistration);
 98
 5099            _serviceCollection.TryAddSingleton(typeof(IConfigureOptions<AzureClientCredentialOptions<TClient>>), typeof(
 50100            _serviceCollection.TryAddSingleton(typeof(IOptionsMonitor<TOptions>), typeof(ClientOptionsMonitor<TClient, T
 50101            _serviceCollection.TryAddSingleton(typeof(ClientOptionsFactory<TClient, TOptions>), typeof(ClientOptionsFact
 50102            _serviceCollection.TryAddSingleton(typeof(IConfigureOptions<TOptions>), typeof(DefaultClientOptionsSetup<TOp
 50103            _serviceCollection.TryAddSingleton(typeof(IAzureClientFactory<TClient>), typeof(AzureClientFactory<TClient, 
 50104            _serviceCollection.TryAddSingleton(
 50105                typeof(TClient),
 68106                provider => provider.GetService<IAzureClientFactory<TClient>>().CreateClient(DefaultClientName));
 107
 50108            return new AzureClientBuilder<TClient, TOptions>(clientRegistration, _serviceCollection);
 109        }
 110
 111        /// <summary>
 112        /// Sets the credential to use by default for all clients.
 113        /// </summary>
 114        /// <param name="tokenCredential">The credential to use.</param>
 115        /// <returns>This instance.</returns>
 116        public AzureClientFactoryBuilder UseCredential(TokenCredential tokenCredential)
 117        {
 8118            return UseCredential(_ => tokenCredential);
 119        }
 120
 121        /// <summary>
 122        /// Sets the credential to use by default for all clients.
 123        /// </summary>
 124        /// <param name="tokenCredentialFactory">The credential factory to use.</param>
 125        /// <returns>This instance.</returns>
 126        public AzureClientFactoryBuilder UseCredential(Func<IServiceProvider, TokenCredential> tokenCredentialFactory)
 127        {
 8128            _serviceCollection.Configure<AzureClientsGlobalOptions>(options => options.CredentialFactory = tokenCredenti
 4129            return this;
 130        }
 131    }
 132}