Skip to content
Draft
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
17 changes: 0 additions & 17 deletions extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public class AWSOptions
/// </summary>
public string ExternalId { get; set; }

/// <summary>
///
/// </summary>
public IAWSCredentialsFactory CredentialsFactory { get; set; }

/// <summary>
/// AWS Credentials used for creating service clients. If this is set it overrides the Profile property.
/// </summary>
Expand Down Expand Up @@ -97,18 +92,6 @@ internal set
/// </summary>
public LoggingSetting Logging { get; set; }

/// <summary>
/// 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
/// and returned.
/// </summary>
/// <typeparam name="T">The service interface that a service client will be created for.</typeparam>
/// <returns>The service client that implements the service interface.</returns>
public T CreateServiceClient<T>() where T : class, IAmazonService
{
return new ClientFactory<T>(this, CredentialsFactory, (ILogger)null).CreateServiceClient() as T;
}

/// <summary>
/// Container for logging settings of the SDK
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Amazon.Extensions.NETCore.Setup;
using Amazon.Runtime;

namespace AWSSDK.Extensions.NETCore.Setup
{
/// <summary>
///
/// </summary>
public static class AWSOptionsExtensions
{
/// <summary>
/// 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
/// and returned.
/// </summary>
/// <typeparam name="T">The service interface that a service client will be created for.</typeparam>
/// <returns>The service client that implements the service interface.</returns>
public static T CreateServiceClient<T>(this AWSOptions options, IAWSCredentialsFactory credentialsFactory = null)
where T : class, IAmazonService
{
credentialsFactory = credentialsFactory ?? new DefaultAWSCredentialsFactory(options);
var clientFactory = new ClientFactory<T>(options, credentialsFactory, null);

return clientFactory.CreateServiceClient() as T;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static IServiceCollection TryAddDefaultAWSOptions(
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddAWSService<T>(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
return AddAWSService<T>(collection, null, lifetime);
return AddAWSService<T>(collection, options: null, lifetime);
}

/// <summary>
Expand All @@ -216,6 +216,23 @@ public static IServiceCollection AddAWSService<T>(this IServiceCollection collec
return collection;
}

/// <summary>
/// Adds the Amazon service client to the dependency injection framework. The Amazon service client is not
/// created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
/// instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="T">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="optionsFunc">A func that resolves the AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddAWSService<T>(this IServiceCollection collection, Func<IServiceProvider, AWSOptions> optionsFunc, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
var descriptor = new ServiceDescriptor(typeof(T), sp => CreateServiceClient<T>(optionsFunc(sp), sp), lifetime);
collection.Add(descriptor);
return collection;
}

/// <summary>
/// Adds the Amazon service client to the dependency injection framework if the service type hasn't already been registered.
/// The Amazon service client is not created until it is requested. If the ServiceLifetime property is set to Singleton,
Expand All @@ -227,7 +244,7 @@ public static IServiceCollection AddAWSService<T>(this IServiceCollection collec
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddAWSService<T>(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
return TryAddAWSService<T>(collection, null, lifetime);
return TryAddAWSService<T>(collection, options: null, lifetime);
}

/// <summary>
Expand All @@ -247,6 +264,23 @@ public static IServiceCollection TryAddAWSService<T>(this IServiceCollection col
return collection;
}

/// <summary>
/// Adds the Amazon service client to the dependency injection framework if the service type hasn't already been registered.
/// The Amazon service client is not created until it is requested. If the ServiceLifetime property is set to Singleton,
/// the default, then the same instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="T">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="optionsFunc">A func that resolves the AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddAWSService<T>(this IServiceCollection collection, Func<IServiceProvider, AWSOptions> optionsFunc, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
var descriptor = new ServiceDescriptor(typeof(T), sp => CreateServiceClient<T>(optionsFunc(sp), sp), lifetime);
collection.TryAdd(descriptor);
return collection;
}

#if NET8_0_OR_GREATER

/// <summary>
Expand All @@ -261,7 +295,7 @@ public static IServiceCollection TryAddAWSService<T>(this IServiceCollection col
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddKeyedAWSService<T>(this IServiceCollection collection, object serviceKey, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
return AddKeyedAWSService<T>(collection, serviceKey, null, lifetime);
return AddKeyedAWSService<T>(collection, serviceKey, options: null, lifetime);
}

/// <summary>
Expand All @@ -281,6 +315,24 @@ public static IServiceCollection AddKeyedAWSService<T>(this IServiceCollection c
collection.Add(descriptor);
return collection;
}

/// <summary>
/// Adds the Amazon service client to the dependency injection framework with a key. The Amazon service client is not
/// created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
/// instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="T">The AWS service interface, like IAmazonS3</typeparam>
/// <param name="collection"></param>
/// <param name="serviceKey">The key with which the service will be added in the dependency injection framework.</param>
/// <param name="optionsFunc">A func that resolves the AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddKeyedAWSService<T>(this IServiceCollection collection, object serviceKey, Func<IServiceProvider, AWSOptions> optionsFunc, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
var descriptor = new ServiceDescriptor(typeof(T), serviceKey, (sp, _) => CreateServiceClient<T>(optionsFunc(sp), sp), lifetime);
collection.Add(descriptor);
return collection;
}

/// <summary>
/// Adds the Amazon service client to the dependency injection framework with a key if the service type hasn't already been registered with the same key.
Expand All @@ -294,7 +346,7 @@ public static IServiceCollection AddKeyedAWSService<T>(this IServiceCollection c
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddKeyedAWSService<T>(this IServiceCollection collection, object serviceKey, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
return TryAddKeyedAWSService<T>(collection, serviceKey, null, lifetime);
return TryAddKeyedAWSService<T>(collection, serviceKey, options: null, lifetime);
}

/// <summary>
Expand All @@ -314,13 +366,31 @@ public static IServiceCollection TryAddKeyedAWSService<T>(this IServiceCollectio
collection.TryAdd(descriptor);
return collection;
}

/// <summary>
/// Adds the Amazon service client to the dependency injection framework with a key if the service type hasn't already been registered with the same key.
/// The Amazon service client is not created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
/// instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="T">The AWS service interface, like IAmazonS3</typeparam>
/// <param name="collection"></param>
/// <param name="serviceKey">The key with which the service will be added in the dependency injection framework.</param>
/// <param name="optionsFunc">A func that resolves the AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddKeyedAWSService<T>(this IServiceCollection collection, object serviceKey, Func<IServiceProvider, AWSOptions> optionsFunc, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
var descriptor = new ServiceDescriptor(typeof(T), serviceKey, (sp, _) => CreateServiceClient<T>(optionsFunc(sp), sp), lifetime);
collection.TryAdd(descriptor);
return collection;
}
#endif

private static object CreateServiceClient<T>(AWSOptions options, IServiceProvider sp) where T : IAmazonService
{
var logger = sp.GetService<ILogger>();
var awsOptions = options ?? sp.GetService<AWSOptions>() ?? new AWSOptions();
var credentialsFactory = awsOptions.CredentialsFactory ?? sp.GetService<IAWSCredentialsFactory>() ?? new DefaultAWSCredentialsFactory(awsOptions, logger);
var credentialsFactory = sp.GetService<IAWSCredentialsFactory>() ?? new DefaultAWSCredentialsFactory(awsOptions, logger);

var factory = new ClientFactory<T>(awsOptions, credentialsFactory, logger);

Expand Down
1 change: 1 addition & 0 deletions extensions/test/NETCore.SetupTests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Amazon;
using Amazon.S3;
using Amazon.Runtime;
using AWSSDK.Extensions.NETCore.Setup;

namespace NETCore.SetupTests
{
Expand Down