|
using System; |
|
using System.Linq; |
|
|
|
namespace Microsoft.Extensions.DependencyInjection |
|
{ |
|
|
|
/// <summary> |
|
/// Extensions methods that for the IServiceCollection object that can be used across many different projects types (Web, Exe, Batch, etc). |
|
/// </summary> |
|
public static class ConvenienceIServiceCollectionExtensions |
|
{ |
|
/// <summary> |
|
/// Tests if the service collection contains a "binding" for the given type. |
|
/// </summary> |
|
/// <param name="services">The collection to read through.</param> |
|
/// <param name="serviceType">The type to find.</param> |
|
/// <returns>True if the type is found. False otherwise.</returns> |
|
public static bool Contains(this IServiceCollection services, Type serviceType) |
|
{ |
|
var serviceDescriptor = services.FirstOrDefault(descriptor => descriptor.ServiceType == serviceType); |
|
return serviceDescriptor != null; |
|
} |
|
|
|
/// <summary> |
|
/// Tests if the service collection contains a "binding" for the given type. |
|
/// </summary> |
|
/// <param name="services">The collection to read through.</param> |
|
/// <param name="serviceType">The type to find.</param> |
|
/// <returns>True if the type is found. False otherwise.</returns> |
|
public static bool Contains<T>(this IServiceCollection services) |
|
{ |
|
return Contains(services, typeof(T)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" from the service collection. This functionality is also available from the standard |
|
/// IServiceCollection (<see cref="IServiceCollection.Remove" />), but you can to find the underlying |
|
/// <see cref="ServiceDescriptor" /> object which contains the binding. The is a convenience overload |
|
/// that does that search and removal for you. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The type binding to remove.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection Remove(this IServiceCollection services, Type serviceType) |
|
{ |
|
var serviceDescriptor = services.FirstOrDefault(descriptor => descriptor.ServiceType == serviceType); |
|
if (serviceDescriptor != null) services.Remove(serviceDescriptor); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" from the service collection. This functionality is also available from the standard |
|
/// IServiceCollection (<see cref="IServiceCollection.Remove" />), but you can to find the underlying |
|
/// <see cref="ServiceDescriptor" /> object which contains the binding. The is a convenience overload |
|
/// that does that search and removal for you. |
|
/// </summary> |
|
/// <typeparam name="T">The type binding to remove.</typeparam> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection Remove<T>(this IServiceCollection services) |
|
where T : class |
|
{ |
|
return services.Remove(typeof(T)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it in the new scope. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindTransient( |
|
this IServiceCollection services, |
|
Type serviceType |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddTransient(serviceType); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it to type <typeparamref name="TImplementation" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <param name="implementationType">The type to bind to.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindTransient( |
|
this IServiceCollection services, |
|
Type serviceType, |
|
Type implementationType |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddTransient(serviceType, implementationType); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it in the new scope. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <typeparam name="TService">The service/interface type to bind for lookup.</typeparam> |
|
/// <typeparam name="TImplementation">The concrete/implementation to bind to.</typeparam> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindTransient<TService>(this IServiceCollection services) |
|
where TService : class |
|
{ |
|
return services.RebindTransient(typeof(TService)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it to type <typeparamref name="TImplementation" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <typeparam name="TService">The service/interface type to bind for lookup.</typeparam> |
|
/// <typeparam name="TImplementation">The concrete/implementation to bind to.</typeparam> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindTransient<TService, TImplementation>(this IServiceCollection services) |
|
where TService : class |
|
where TImplementation : class, TService |
|
{ |
|
return services.RebindTransient(typeof(TService), typeof(TImplementation)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to the type |
|
/// returned from <paramref name="implementationFactory" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <param name="implementationFactory">A factory to generate the requested service/interface type.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindTransient( |
|
this IServiceCollection services, |
|
Type serviceType, |
|
Func<IServiceProvider, object> implementationFactory |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddTransient(serviceType, implementationFactory); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to the type |
|
/// returned from <paramref name="implementationFactory" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="implementationFactory">A factory to generate the requested service/interface type.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindTransient<T>( |
|
this IServiceCollection services, |
|
Func<IServiceProvider, object> implementationFactory |
|
) |
|
{ |
|
return RebindTransient(services, typeof(T), implementationFactory); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it in the new scope. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddSingleton{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <param name="implementationType">The type to bind to.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindSingleton( |
|
this IServiceCollection services, |
|
Type serviceType |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddSingleton(serviceType); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to type <paramref name="implementationType" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddSingleton{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <param name="implementationType">The type to bind to.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindSingleton( |
|
this IServiceCollection services, |
|
Type serviceType, |
|
Type implementationType |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddSingleton(serviceType, implementationType); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it in the new scope. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddSingleton{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <typeparam name="TService">The service/interface type to bind for lookup.</typeparam> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindSingleton<TService>(this IServiceCollection services) |
|
where TService : class |
|
{ |
|
return services.RebindSingleton(typeof(TService)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it to type <typeparamref name="TImplementation" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddSingleton{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <typeparam name="TService">The service/interface type to bind for lookup.</typeparam> |
|
/// <typeparam name="TImplementation">The concrete/implementation to bind to.</typeparam> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindSingleton<TService, TImplementation>(this IServiceCollection services) |
|
where TService : class |
|
where TImplementation : class, TService |
|
{ |
|
return services.RebindSingleton(typeof(TService), typeof(TImplementation)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to the type |
|
/// returned from <paramref name="implementationFactory" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddSingleton{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <param name="implementationFactory">A factory to generate the requested service/interface type.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindSingleton( |
|
this IServiceCollection services, |
|
Type serviceType, |
|
Func<IServiceProvider, object> implementationFactory |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddSingleton(serviceType, implementationFactory); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to the type |
|
/// returned from <paramref name="implementationFactory" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="implementationFactory">A factory to generate the requested service/interface type.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindSingleton<T>( |
|
this IServiceCollection services, |
|
Func<IServiceProvider, object> implementationFactory |
|
) |
|
{ |
|
return RebindSingleton(services, typeof(T), implementationFactory); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to the type |
|
/// returned from <paramref name="implementationFactory" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="implementationInstance">A instance of type T.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindSingleton<T>( |
|
this IServiceCollection services, |
|
T implementationInstance |
|
) where T : class |
|
{ |
|
services.Remove(typeof(T)); |
|
services.AddSingleton<T>(implementationInstance); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it in the new scope. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddScoped{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindScoped( |
|
this IServiceCollection services, |
|
Type serviceType |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddScoped(serviceType); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to type <paramref name="implementationType" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddScoped{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <param name="implementationType">The type to bind to.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindScoped( |
|
this IServiceCollection services, |
|
Type serviceType, |
|
Type implementationType |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddScoped(serviceType, implementationType); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it in the new scope. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddScoped{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <typeparam name="TService">The service/interface type to bind for lookup.</typeparam> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindScoped<TService>(this IServiceCollection services) |
|
where TService : class |
|
{ |
|
return services.RebindScoped(typeof(TService)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <typeparamref name="TService" /> and rebinds it to type <typeparamref name="TImplementation" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddScoped{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <typeparam name="TService">The service/interface type to bind for lookup.</typeparam> |
|
/// <typeparam name="TImplementation">The concrete/implementation to bind to.</typeparam> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindScoped<TService, TImplementation>(this IServiceCollection services) |
|
where TService : class |
|
where TImplementation : class, TService |
|
{ |
|
return services.RebindScoped(typeof(TService), typeof(TImplementation)); |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to the type |
|
/// returned from <paramref name="implementationFactory" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddScoped{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="serviceType">The service/interface type to bind for lookup.</param> |
|
/// <param name="implementationFactory">A factory to generate the requested service/interface type.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindScoped( |
|
this IServiceCollection services, |
|
Type serviceType, |
|
Func<IServiceProvider, object> implementationFactory |
|
) |
|
{ |
|
services.Remove(serviceType); |
|
services.AddScoped(serviceType, implementationFactory); |
|
|
|
return services; |
|
} |
|
|
|
/// <summary> |
|
/// Removes a "binding" for the type <paramref name="serviceType" /> and rebinds it to the type |
|
/// returned from <paramref name="implementationFactory" />. |
|
/// This is a convenience overload which calls <see cref="Remove{T}" /> and then |
|
/// <see cref="ServiceCollectionServiceExtensions.AddTransient{TService, TImplementation}" />. |
|
/// </summary> |
|
/// <param name="services">The <see cref="IServiceCollection" /> object to update.</param> |
|
/// <param name="implementationFactory">A factory to generate the requested service/interface type.</param> |
|
/// <returns>The <see cref="IServiceCollection" /> object which was referenced/passed in.</returns> |
|
public static IServiceCollection RebindScoped<T>( |
|
this IServiceCollection services, |
|
Func<IServiceProvider, object> implementationFactory |
|
) |
|
{ |
|
return RebindScoped(services, typeof(T), implementationFactory); |
|
} |
|
|
|
|
|
} |
|
} |