/// <summary> /// Creates a storage client /// </summary> /// <param name="configuration">The <see cref="IConfiguration"/> to use when creating Client-specific objects.</param> /// <param name="tokenCredential">The <see cref="TokenCredential"/> to authenticate for requests.</param> /// <param name="options">Generic options to use for the client</param> /// <returns>Storage client</returns> protected virtual TClient CreateClient(IConfiguration configuration, TokenCredential tokenCredential, TClientOptions options) { // If connection string is present, it will be honored first if (!IsConnectionStringPresent(configuration) && TryGetServiceUri(configuration, out Uri serviceUri)) { var constructor = typeof(TClient).GetConstructor(new Type[] { typeof(Uri), typeof(TokenCredential), typeof(TClientOptions) }); return((TClient)constructor.Invoke(new object[] { serviceUri, tokenCredential, options })); } return((TClient)_componentFactory.CreateClient(typeof(TClient), configuration, tokenCredential, options)); }
public void CanCreateClientWithoutRegistrationUsingConnectionString() { var configuration = GetConfiguration( new KeyValuePair <string, string>("TestClient", "http://localhost/")); var serviceCollection = new ServiceCollection(); serviceCollection.AddAzureClientsCore(); ServiceProvider provider = serviceCollection.BuildServiceProvider(); AzureComponentFactory factory = provider.GetService <AzureComponentFactory>(); TestClient client = (TestClient)factory.CreateClient(typeof(TestClient), configuration.GetSection("TestClient"), null, new TestClientOptions()); Assert.AreEqual("http://localhost/", client.ConnectionString); }
public void CanCreateClientWithoutRegistration() { var configuration = GetConfiguration( new KeyValuePair <string, string>("TestClient:uri", "http://localhost/")); var serviceCollection = new ServiceCollection(); serviceCollection.AddAzureClientsCore(); ServiceProvider provider = serviceCollection.BuildServiceProvider(); AzureComponentFactory factory = provider.GetService <AzureComponentFactory>(); TestClientWithCredentials client = (TestClientWithCredentials)factory.CreateClient(typeof(TestClientWithCredentials), configuration.GetSection("TestClient"), new EnvironmentCredential(), new TestClientOptions()); Assert.AreEqual("http://localhost/", client.Uri.ToString()); Assert.IsInstanceOf <EnvironmentCredential>(client.Credential); }
public Task <object> GetValueAsync() { Type clientOptionType = null; foreach (var constructor in _clientType.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"); } var credential = _componentFactory.CreateTokenCredential(_configuration); var options = _componentFactory.CreateClientOptions(clientOptionType, null, _configuration); return(Task.FromResult(_componentFactory.CreateClient(_clientType, _configuration, credential, options))); }
protected virtual TClient CreateClient(IConfiguration configuration, TokenCredential tokenCredential, TClientOptions options) { return((TClient)_componentFactory.CreateClient(typeof(TClient), configuration, tokenCredential, options)); }