示例#1
0
        public FallbackAzureClientFactory(
            IOptionsMonitor <AzureClientsGlobalOptions> globalOptions,
            IServiceProvider serviceProvider,
            EventSourceLogForwarder logForwarder)
        {
            _serviceProvider   = serviceProvider;
            _globalOptions     = globalOptions.CurrentValue;
            _configurationRoot = _globalOptions.ConfigurationRootResolver?.Invoke(_serviceProvider);

            _logForwarder        = logForwarder;
            _clientRegistrations = new Dictionary <string, FallbackClientRegistration <TClient> >();

            foreach (var constructor in typeof(TClient).GetConstructors(BindingFlags.Public | BindingFlags.Instance))
            {
                var lastParameter = constructor.GetParameters().LastOrDefault();
                if (lastParameter != null && typeof(ClientOptions).IsAssignableFrom(lastParameter.ParameterType))
                {
                    _clientOptionType = lastParameter.ParameterType;
                    break;
                }
            }

            if (_clientOptionType == null)
            {
                throw new InvalidOperationException("Unable to detect the client option type");
            }

            _optionsFactory = (IClientOptionsFactory)ActivatorUtilities.CreateInstance(serviceProvider, typeof(ClientOptionsFactory <,>).MakeGenericType(typeof(TClient), _clientOptionType));
        }
示例#2
0
        private object GetClientOptions(AzureClientsGlobalOptions globalOptions, IConfiguration section)
        {
            var clientOptions = (ClientOptions)ClientFactory.CreateClientOptions(null, _clientOptionType);

            foreach (var globalConfigureOptions in globalOptions.ConfigureOptionDelegates)
            {
                globalConfigureOptions(clientOptions, _serviceProvider);
            }

            section.Bind(clientOptions);

            return(clientOptions);
        }