| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using Azure.Core.Extensions; |
| | 6 | | using Microsoft.Extensions.Configuration; |
| | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | 8 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | 9 | | using Microsoft.Extensions.Options; |
| | 10 | | using System; |
| | 11 | |
|
| | 12 | | namespace 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 | |
|
| 44 | 23 | | internal AzureClientFactoryBuilder(IServiceCollection serviceCollection) |
| | 24 | | { |
| 44 | 25 | | _serviceCollection = serviceCollection; |
| 44 | 26 | | _serviceCollection.AddOptions(); |
| 44 | 27 | | _serviceCollection.TryAddSingleton<EventSourceLogForwarder>(); |
| 44 | 28 | | } |
| | 29 | |
|
| | 30 | | IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilder.RegisterClientFactory<TClient, TOptions>(Func< |
| | 31 | | { |
| 62 | 32 | | return ((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions>((options, _ |
| | 33 | | } |
| | 34 | |
|
| | 35 | | IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithConfiguration<IConfiguration>.RegisterClien |
| | 36 | | { |
| 6 | 37 | | var credentialsFromConfig = ClientFactory.CreateCredential(configuration); |
| 6 | 38 | | var clientBuilder =((IAzureClientFactoryBuilderWithCredential)this).RegisterClientFactory<TClient, TOptions> |
| 12 | 39 | | (options, credentials) => (TClient)ClientFactory.CreateClient(typeof(TClient), typeof(TOptions), options |
| 6 | 40 | | .ConfigureOptions(configuration); |
| | 41 | |
|
| 6 | 42 | | if (credentialsFromConfig != null) |
| | 43 | | { |
| 2 | 44 | | clientBuilder.WithCredential(credentialsFromConfig); |
| | 45 | | } |
| | 46 | |
|
| 6 | 47 | | 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 | | { |
| 12 | 57 | | ConfigureDefaults((options, provider) => configureOptions(options)); |
| 6 | 58 | | 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 | | { |
| 12 | 68 | | _serviceCollection.Configure<AzureClientsGlobalOptions>(options => options.ConfigureOptionDelegates.Add(conf |
| | 69 | |
|
| 6 | 70 | | 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 | | { |
| 8 | 80 | | ConfigureDefaults(options => configuration.Bind(options)); |
| | 81 | |
|
| 4 | 82 | | var credentialsFromConfig = ClientFactory.CreateCredential(configuration); |
| | 83 | |
|
| 4 | 84 | | if (credentialsFromConfig != null) |
| | 85 | | { |
| 2 | 86 | | UseCredential(credentialsFromConfig); |
| | 87 | | } |
| | 88 | |
|
| 4 | 89 | | return this; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | IAzureClientBuilder<TClient, TOptions> IAzureClientFactoryBuilderWithCredential.RegisterClientFactory<TClient, T |
| | 93 | | { |
| 50 | 94 | | var clientRegistration = new ClientRegistration<TClient, TOptions>(DefaultClientName, clientFactory); |
| 50 | 95 | | clientRegistration.RequiresTokenCredential = requiresCredential; |
| | 96 | |
|
| 50 | 97 | | _serviceCollection.AddSingleton(clientRegistration); |
| | 98 | |
|
| 50 | 99 | | _serviceCollection.TryAddSingleton(typeof(IConfigureOptions<AzureClientCredentialOptions<TClient>>), typeof( |
| 50 | 100 | | _serviceCollection.TryAddSingleton(typeof(IOptionsMonitor<TOptions>), typeof(ClientOptionsMonitor<TClient, T |
| 50 | 101 | | _serviceCollection.TryAddSingleton(typeof(ClientOptionsFactory<TClient, TOptions>), typeof(ClientOptionsFact |
| 50 | 102 | | _serviceCollection.TryAddSingleton(typeof(IConfigureOptions<TOptions>), typeof(DefaultClientOptionsSetup<TOp |
| 50 | 103 | | _serviceCollection.TryAddSingleton(typeof(IAzureClientFactory<TClient>), typeof(AzureClientFactory<TClient, |
| 50 | 104 | | _serviceCollection.TryAddSingleton( |
| 50 | 105 | | typeof(TClient), |
| 68 | 106 | | provider => provider.GetService<IAzureClientFactory<TClient>>().CreateClient(DefaultClientName)); |
| | 107 | |
|
| 50 | 108 | | 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 | | { |
| 8 | 118 | | 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 | | { |
| 8 | 128 | | _serviceCollection.Configure<AzureClientsGlobalOptions>(options => options.CredentialFactory = tokenCredenti |
| 4 | 129 | | return this; |
| | 130 | | } |
| | 131 | | } |
| | 132 | | } |