/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Amazon.Runtime;
using Amazon.Extensions.NETCore.Setup;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection
{
///
/// This class adds extension methods to IServiceCollection making it easier to add Amazon service clients
/// to the NET Core dependency injection framework.
///
public static class ServiceCollectionExtensions
{
///
/// Adds the AWSOptions object to the dependency injection framework providing information
/// that will be used to construct Amazon service clients.
///
///
/// The default AWS options used to construct AWS service clients with.
/// Returns back the IServiceCollection to continue the fluent system of IServiceCollection.
public static IServiceCollection AddDefaultAWSOptions(this IServiceCollection collection, AWSOptions options)
{
collection.Add(new ServiceDescriptor(typeof(AWSOptions), options));
return collection;
}
///
/// Adds the AWSOptions object to the dependency injection framework providing information
/// that will be used to construct Amazon service clients.
///
///
/// The factory that creates the default AWS options.
/// The AWS options will be used to construct AWS service clients
///
/// The lifetime of the AWSOptions. The default is Singleton.
/// Returns back the IServiceCollection to continue the fluent system of IServiceCollection.
public static IServiceCollection AddDefaultAWSOptions(
this IServiceCollection collection,
Func implementationFactory,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.Add(new ServiceDescriptor(typeof(AWSOptions), implementationFactory, lifetime));
return collection;
}
///
/// 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.
///
/// The AWS service interface, like IAmazonS3.
///
/// The lifetime of the service client created. The default is Singleton.
/// Returns back the IServiceCollection to continue the fluent system of IServiceCollection.
public static IServiceCollection AddAWSService(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
return AddAWSService(collection, null, lifetime);
}
///
/// 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.
///
/// The AWS service interface, like IAmazonS3.
///
/// The AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.
/// The lifetime of the service client created. The default is Singleton.
/// Returns back the IServiceCollection to continue the fluent system of IServiceCollection.
public static IServiceCollection AddAWSService(this IServiceCollection collection, AWSOptions options, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
Func factory =
new ClientFactory(typeof(T), options).CreateServiceClient;
var descriptor = new ServiceDescriptor(typeof(T), factory, lifetime);
collection.Add(descriptor);
return collection;
}
///
/// 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.
///
/// The AWS service interface, like IAmazonS3.
///
/// The lifetime of the service client created. The default is Singleton.
/// Returns back the IServiceCollection to continue the fluent system of IServiceCollection.
public static IServiceCollection TryAddAWSService(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
return TryAddAWSService(collection, null, lifetime);
}
///
/// 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.
///
/// The AWS service interface, like IAmazonS3.
///
/// The AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.
/// The lifetime of the service client created. The default is Singleton.
/// Returns back the IServiceCollection to continue the fluent system of IServiceCollection.
public static IServiceCollection TryAddAWSService(this IServiceCollection collection, AWSOptions options, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : IAmazonService
{
Func factory =
new ClientFactory(typeof(T), options).CreateServiceClient;
var descriptor = new ServiceDescriptor(typeof(T), factory, lifetime);
collection.TryAdd(descriptor);
return collection;
}
}
}