Skip to content

Support to use Func<IServiceProvider, AWSOptions> to create AWSOptions object. #1958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public static class ServiceCollectionExtensions
{

/// <summary>
/// Adds the AWSOptions object to the dependency injection framework providing information
/// that will be used to construct Amazon service clients.
/// </summary>
/// <param name="collection"></param>
/// <param name="implementationFactory">The factory that creates the service AWSOptions</param>
/// <param name="lifetime">The lifetime of the AWSOptions. The default is Singleton.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddDefaultAWSOptions(this IServiceCollection collection, Func<IServiceProvider, AWSOptions> implementationFactory, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.Add(new ServiceDescriptor(typeof(AWSOptions), implementationFactory, lifetime));
return collection;
}

/// <summary>
/// Adds the AWSOptions object to the dependency injection framework providing information
/// that will be used to construct Amazon service clients.
Expand Down
66 changes: 66 additions & 0 deletions extensions/test/NETCore.SetupTests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,66 @@ public void TryAddServiceDontOverrideWhenAlreadySetup()
Assert.True(s3.GetType() == mockS3.GetType());
}

[Fact]
public void CreateAWSOptionsByDIObjects()
{
RegionEndpoint expectRegion = RegionEndpoint.APSouth1;
string expectProfile = "MockProfile";

ServiceCollection services = new ServiceCollection();
services.AddSingleton(new AWSSetting() {
Region = expectRegion,
Profile = expectProfile
});

services.AddDefaultAWSOptions(sp=> {
var setting = sp.GetRequiredService<AWSSetting>();
return new AWSOptions()
{
Region = setting.Region,
Profile = setting.Profile
};
});

var serviceProvider = services.BuildServiceProvider();
var awsOptions = serviceProvider.GetRequiredService<AWSOptions>();

Assert.NotNull(awsOptions);
Assert.Equal(expectRegion, awsOptions.Region);
Assert.Equal(expectProfile, awsOptions.Profile);
}

[Fact]
public void InjectS3ClientByDIConfigs()
{
RegionEndpoint expectRegion = RegionEndpoint.APSouth1;
string expectProfile = "MockProfile";

ServiceCollection services = new ServiceCollection();
services.AddSingleton(new AWSSetting()
{
Region = expectRegion,
Profile = expectProfile
});

services.AddDefaultAWSOptions(sp => {
var setting = sp.GetRequiredService<AWSSetting>();
return new AWSOptions()
{
Region = setting.Region,
Profile = setting.Profile
};
});

services.AddAWSService<IAmazonS3>();

var serviceProvider = services.BuildServiceProvider();

var controller = ActivatorUtilities.CreateInstance<TestController>(serviceProvider);
Assert.NotNull(controller.S3Client);
Assert.Equal(expectRegion, controller.S3Client.Config.RegionEndpoint);
}

public class TestController
{
public IAmazonS3 S3Client { get; private set; }
Expand All @@ -82,5 +142,11 @@ public TestController(IAmazonS3 s3Client)
S3Client = s3Client;
}
}

internal class AWSSetting {
public RegionEndpoint Region { get; set; }

public string Profile { get; set; }
}
}
}