diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptions.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptions.cs index 74bd98b4eabc..cbb3ce19d994 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptions.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptions.cs @@ -81,6 +81,10 @@ public ClientConfig DefaultClientConfig return this._defaultClientConfig; } + internal set + { + this._defaultClientConfig = value; + } } /// @@ -89,14 +93,6 @@ public ClientConfig DefaultClientConfig /// public LoggingSetting Logging { get; set; } - internal bool IsDefaultClientConfigSet - { - get - { - return this._defaultClientConfig != null; - } - } - /// /// Create a service client for the specified service interface using the options set in this instance. /// For example if T is set to IAmazonS3 then the AmazonS3ServiceClient which implements IAmazonS3 is created diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs index 93b5a2ee916d..ba1eaea2acf5 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs @@ -219,39 +219,37 @@ private static ClientConfig CreateConfig(Type serviceInterfaceType, AWSOptions o } var defaultConfig = options.DefaultClientConfig; - if (options.IsDefaultClientConfigSet) - { - var emptyArray = new object[0]; - var singleArray = new object[1]; + var emptyArray = new object[0]; + var singleArray = new object[1]; - var clientConfigTypeInfo = typeof(ClientConfig).GetTypeInfo(); - foreach (var property in clientConfigTypeInfo.DeclaredProperties) + var clientConfigTypeInfo = options.DefaultClientConfig.GetType(); + var properties = clientConfigTypeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance); + foreach (var property in properties) + { + if (property.GetMethod != null && property.SetMethod != null) { - if (property.GetMethod != null && property.SetMethod != null) - { - // Skip RegionEndpoint because it is set below and calling the get method on the - // property triggers the default region fallback mechanism. - if (string.Equals(property.Name, "RegionEndpoint", StringComparison.Ordinal)) - continue; + // Skip RegionEndpoint because it is set below and calling the get method on the + // property triggers the default region fallback mechanism. + if (string.Equals(property.Name, "RegionEndpoint", StringComparison.Ordinal)) + continue; - // DefaultConfigurationMode is skipped from the DefaultClientConfig because it is expected to be set - // at the top level of AWSOptions which is done before this loop. - if (string.Equals(property.Name, "DefaultConfigurationMode", StringComparison.Ordinal)) - continue; + // DefaultConfigurationMode is skipped from the DefaultClientConfig because it is expected to be set + // at the top level of AWSOptions which is done before this loop. + if (string.Equals(property.Name, "DefaultConfigurationMode", StringComparison.Ordinal)) + continue; - // Skip setting RetryMode if it is set to legacy but the DefaultConfigurationMode is not legacy. - // This will allow the retry mode to be configured from the DefaultConfiguration. - // This is a workaround to handle the inability to tell if RetryMode was explicitly set. - if (string.Equals(property.Name, "RetryMode", StringComparison.Ordinal) && - defaultConfig.RetryMode == RequestRetryMode.Legacy && - config.DefaultConfigurationMode != DefaultConfigurationMode.Legacy) - continue; + // Skip setting RetryMode if it is set to legacy but the DefaultConfigurationMode is not legacy. + // This will allow the retry mode to be configured from the DefaultConfiguration. + // This is a workaround to handle the inability to tell if RetryMode was explicitly set. + if (string.Equals(property.Name, "RetryMode", StringComparison.Ordinal) && + defaultConfig.RetryMode == RequestRetryMode.Legacy && + config.DefaultConfigurationMode != DefaultConfigurationMode.Legacy) + continue; - singleArray[0] = property.GetMethod.Invoke(defaultConfig, emptyArray); - if (singleArray[0] != null) - { - property.SetMethod.Invoke(config, singleArray); - } + singleArray[0] = property.GetMethod.Invoke(defaultConfig, emptyArray); + if (singleArray[0] != null) + { + property.SetMethod.Invoke(config, singleArray); } } } diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ConfigurationExtensions.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ConfigurationExtensions.cs index 9f6e0d614851..621a2c411891 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ConfigurationExtensions.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ConfigurationExtensions.cs @@ -53,7 +53,33 @@ public static AWSOptions GetAWSOptions(this IConfiguration config) /// The AWSOptions containing the values set in configuration system. public static AWSOptions GetAWSOptions(this IConfiguration config, string configSection) { - var options = new AWSOptions(); + return GetAWSOptions(config, configSection); + } + + /// + /// Constructs an AWSOptions class with the options specified in the "AWS" section in the IConfiguration object. + /// + /// The AWS client config to be used in creating clients, like AmazonS3Config. + /// + /// The AWSOptions containing the values set in configuration system. + public static AWSOptions GetAWSOptions(this IConfiguration config) where TConfig : ClientConfig, new() + { + return GetAWSOptions(config, DEFAULT_CONFIG_SECTION); + } + + /// + /// Constructs an AWSOptions class with the options specified in the "AWS" section in the IConfiguration object. + /// + /// The AWS client config to be used in creating clients, like AmazonS3Config. + /// + /// The config section to extract AWS options from. + /// The AWSOptions containing the values set in configuration system. + public static AWSOptions GetAWSOptions(this IConfiguration config, string configSection) where TConfig : ClientConfig, new() + { + var options = new AWSOptions + { + DefaultClientConfig = new TConfig(), + }; IConfiguration section; if (string.IsNullOrEmpty(configSection)) @@ -64,12 +90,13 @@ public static AWSOptions GetAWSOptions(this IConfiguration config, string config if (section == null) return options; - var clientConfigTypeInfo = typeof(ClientConfig).GetTypeInfo(); - foreach(var element in section.GetChildren()) + var clientConfigType = typeof(TConfig); + var properties = clientConfigType.GetProperties(BindingFlags.Public | BindingFlags.Instance); + foreach (var element in section.GetChildren()) { try { - var property = clientConfigTypeInfo.DeclaredProperties.SingleOrDefault(p => p.Name.Equals(element.Key, StringComparison.OrdinalIgnoreCase)); + var property = properties.SingleOrDefault(p => p.Name.Equals(element.Key, StringComparison.OrdinalIgnoreCase)); if (property == null || property.SetMethod == null) continue; diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultClientConfig.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultClientConfig.cs index 3fe3abc424df..d369be5e797b 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultClientConfig.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultClientConfig.cs @@ -26,7 +26,7 @@ namespace Amazon.Extensions.NETCore.Setup /// internal class DefaultClientConfig : ClientConfig { - internal DefaultClientConfig() + public DefaultClientConfig() { } diff --git a/extensions/test/NETCore.SetupTests/ConfigurationTests.cs b/extensions/test/NETCore.SetupTests/ConfigurationTests.cs index ef7de654913f..27da89881326 100644 --- a/extensions/test/NETCore.SetupTests/ConfigurationTests.cs +++ b/extensions/test/NETCore.SetupTests/ConfigurationTests.cs @@ -121,6 +121,11 @@ public void GetClientConfigSettingsTest() client = options.CreateServiceClient(); Assert.Equal(DefaultConfigurationMode.Standard, client.Config.DefaultConfigurationMode); Assert.Equal(RequestRetryMode.Adaptive, client.Config.RetryMode); + + // verify S3 config specific settings are not configured + var clientConfig = client.Config as AmazonS3Config; + Assert.NotNull(clientConfig); + Assert.False(clientConfig.ForcePathStyle); } [Fact] @@ -204,5 +209,43 @@ public void TestRegionFoundFromEnvironmentVariable() FallbackRegionFactory.Reset(); } } + + [Fact] + public void ClientConfigTypeOverloadTest() + { + var config = new ConfigurationBuilder() + .AddJsonFile("./TestFiles/GetClientConfigSettingsTest.json") + .Build(); + + var options = config.GetAWSOptions(); + + Assert.Equal(RegionEndpoint.USWest2, options.Region); + Assert.True(options.DefaultClientConfig.UseHttp); + Assert.Equal(6, options.DefaultClientConfig.MaxErrorRetry); + Assert.Equal(TimeSpan.FromMilliseconds(1000), options.DefaultClientConfig.Timeout); + Assert.Equal("us-east-1", options.DefaultClientConfig.AuthenticationRegion); + Assert.Equal(DefaultConfigurationMode.Standard, options.DefaultConfigurationMode); + + var client = options.CreateServiceClient(); + Assert.NotNull(client); + Assert.Equal(RegionEndpoint.USWest2, client.Config.RegionEndpoint); + Assert.True(client.Config.UseHttp); + Assert.Equal(6, client.Config.MaxErrorRetry); + Assert.Equal(TimeSpan.FromMilliseconds(1000), client.Config.Timeout); + Assert.Equal("us-east-1", client.Config.AuthenticationRegion); + Assert.Equal(DefaultConfigurationMode.Standard, client.Config.DefaultConfigurationMode); + Assert.Equal(RequestRetryMode.Standard, client.Config.RetryMode); + + // Verify that setting the standard mode doesn't override explicit settings of retry mode to a non-legacy mode. + options.DefaultClientConfig.RetryMode = RequestRetryMode.Adaptive; + client = options.CreateServiceClient(); + Assert.Equal(DefaultConfigurationMode.Standard, client.Config.DefaultConfigurationMode); + Assert.Equal(RequestRetryMode.Adaptive, client.Config.RetryMode); + + // verify S3 config specific settings are configured + var clientConfig = client.Config as AmazonS3Config; + Assert.NotNull(clientConfig); + Assert.True(clientConfig.ForcePathStyle); + } } } diff --git a/extensions/test/NETCore.SetupTests/TestFiles/GetClientConfigSettingsTest.json b/extensions/test/NETCore.SetupTests/TestFiles/GetClientConfigSettingsTest.json index 68584492b6ab..484c2347267a 100644 --- a/extensions/test/NETCore.SetupTests/TestFiles/GetClientConfigSettingsTest.json +++ b/extensions/test/NETCore.SetupTests/TestFiles/GetClientConfigSettingsTest.json @@ -6,6 +6,7 @@ "AuthenticationRegion": "us-east-1", "Region": "us-west-2", "DefaultsMode": "Standard", + "ForcePathStyle": true, "RetryMode": "Standard" } }