| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using Microsoft.Extensions.Options; |
| | 7 | |
|
| | 8 | | namespace Microsoft.Extensions.Azure |
| | 9 | | { |
| | 10 | | internal class AzureClientFactory<TClient, TOptions>: IAzureClientFactory<TClient> |
| | 11 | | { |
| | 12 | | private readonly Dictionary<string, ClientRegistration<TClient, TOptions>> _clientRegistrations; |
| | 13 | |
|
| | 14 | | private readonly IServiceProvider _serviceProvider; |
| | 15 | |
|
| | 16 | | private readonly IOptionsMonitor<AzureClientCredentialOptions<TClient>> _clientsOptions; |
| | 17 | |
|
| | 18 | | private readonly IOptionsMonitor<TOptions> _monitor; |
| | 19 | |
|
| | 20 | | private readonly EventSourceLogForwarder _logForwarder; |
| | 21 | |
|
| 46 | 22 | | public AzureClientFactory( |
| 46 | 23 | | IServiceProvider serviceProvider, |
| 46 | 24 | | IOptionsMonitor<AzureClientCredentialOptions<TClient>> clientsOptions, |
| 46 | 25 | | IEnumerable<ClientRegistration<TClient, TOptions>> clientRegistrations, IOptionsMonitor<TOptions> monitor, |
| 46 | 26 | | EventSourceLogForwarder logForwarder) |
| | 27 | | { |
| 46 | 28 | | _clientRegistrations = new Dictionary<string, ClientRegistration<TClient, TOptions>>(); |
| 192 | 29 | | foreach (var registration in clientRegistrations) |
| | 30 | | { |
| 50 | 31 | | _clientRegistrations[registration.Name] = registration; |
| | 32 | | } |
| | 33 | |
|
| 46 | 34 | | _serviceProvider = serviceProvider; |
| 46 | 35 | | _clientsOptions = clientsOptions; |
| 46 | 36 | | _monitor = monitor; |
| 46 | 37 | | _logForwarder = logForwarder; |
| 46 | 38 | | } |
| | 39 | |
|
| | 40 | | public TClient CreateClient(string name) |
| | 41 | | { |
| 52 | 42 | | if (!_clientRegistrations.TryGetValue(name, out ClientRegistration<TClient, TOptions> registration)) |
| | 43 | | { |
| 2 | 44 | | throw new InvalidOperationException($"Unable to find client registration with type '{typeof(TClient).Nam |
| | 45 | | } |
| | 46 | |
|
| 50 | 47 | | return registration.GetClient(_monitor.Get(name), _clientsOptions.Get(name).CredentialFactory(_serviceProvid |
| | 48 | | } |
| | 49 | | } |
| | 50 | | } |