diff --git a/src/libraries/Common/src/Extensions/ActivatorUtilities/ActivatorUtilities.cs b/src/libraries/Common/src/Extensions/ActivatorUtilities/ActivatorUtilities.cs
new file mode 100644
index 00000000000..e2553ced1a2
--- /dev/null
+++ b/src/libraries/Common/src/Extensions/ActivatorUtilities/ActivatorUtilities.cs
@@ -0,0 +1,429 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+using System.Runtime.ExceptionServices;
+
+#if ActivatorUtilities_In_DependencyInjection
+using Microsoft.Extensions.Internal;
+
+namespace Microsoft.Extensions.DependencyInjection
+#else
+namespace Microsoft.Extensions.Internal
+#endif
+{
+ ///
+ /// Helper code for the various activator services.
+ ///
+
+#if ActivatorUtilities_In_DependencyInjection
+ public
+#else
+ // Do not take a dependency on this class unless you are explicitly trying to avoid taking a
+ // dependency on Microsoft.AspNetCore.DependencyInjection.Abstractions.
+ internal
+#endif
+ static class ActivatorUtilities
+ {
+ private static readonly MethodInfo GetServiceInfo =
+ GetMethodInfo>((sp, t, r, c) => GetService(sp, t, r, c));
+
+ ///
+ /// Instantiate a type with constructor arguments provided directly and/or from an .
+ ///
+ /// The service provider used to resolve dependencies
+ /// The type to activate
+ /// Constructor arguments not provided by the .
+ /// An activated object of type instanceType
+ public static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
+ {
+ int bestLength = -1;
+ var seenPreferred = false;
+
+ ConstructorMatcher bestMatcher = null;
+
+ if (!instanceType.GetTypeInfo().IsAbstract)
+ {
+ foreach (var constructor in instanceType
+ .GetTypeInfo()
+ .DeclaredConstructors
+ .Where(c => !c.IsStatic && c.IsPublic))
+ {
+ var matcher = new ConstructorMatcher(constructor);
+ var isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
+ var length = matcher.Match(parameters);
+
+ if (isPreferred)
+ {
+ if (seenPreferred)
+ {
+ ThrowMultipleCtorsMarkedWithAttributeException();
+ }
+
+ if (length == -1)
+ {
+ ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
+ }
+ }
+
+ if (isPreferred || bestLength < length)
+ {
+ bestLength = length;
+ bestMatcher = matcher;
+ }
+
+ seenPreferred |= isPreferred;
+ }
+ }
+
+ if (bestMatcher == null)
+ {
+ var message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.";
+ throw new InvalidOperationException(message);
+ }
+
+ return bestMatcher.CreateInstance(provider);
+ }
+
+ ///
+ /// Create a delegate that will instantiate a type with constructor arguments provided directly
+ /// and/or from an .
+ ///
+ /// The type to activate
+ ///
+ /// The types of objects, in order, that will be passed to the returned function as its second parameter
+ ///
+ ///
+ /// A factory that will instantiate instanceType using an
+ /// and an argument array containing objects matching the types defined in argumentTypes
+ ///
+ public static ObjectFactory CreateFactory(Type instanceType, Type[] argumentTypes)
+ {
+ FindApplicableConstructor(instanceType, argumentTypes, out ConstructorInfo constructor, out int?[] parameterMap);
+
+ var provider = Expression.Parameter(typeof(IServiceProvider), "provider");
+ var argumentArray = Expression.Parameter(typeof(object[]), "argumentArray");
+ var factoryExpressionBody = BuildFactoryExpression(constructor, parameterMap, provider, argumentArray);
+
+ var factoryLamda = Expression.Lambda>(
+ factoryExpressionBody, provider, argumentArray);
+
+ var result = factoryLamda.Compile();
+ return result.Invoke;
+ }
+
+ ///
+ /// Instantiate a type with constructor arguments provided directly and/or from an .
+ ///
+ /// The type to activate
+ /// The service provider used to resolve dependencies
+ /// Constructor arguments not provided by the .
+ /// An activated object of type T
+ public static T CreateInstance(IServiceProvider provider, params object[] parameters)
+ {
+ return (T)CreateInstance(provider, typeof(T), parameters);
+ }
+
+
+ ///
+ /// Retrieve an instance of the given type from the service provider. If one is not found then instantiate it directly.
+ ///
+ /// The type of the service
+ /// The service provider used to resolve dependencies
+ /// The resolved service or created instance
+ public static T GetServiceOrCreateInstance(IServiceProvider provider)
+ {
+ return (T)GetServiceOrCreateInstance(provider, typeof(T));
+ }
+
+ ///
+ /// Retrieve an instance of the given type from the service provider. If one is not found then instantiate it directly.
+ ///
+ /// The service provider
+ /// The type of the service
+ /// The resolved service or created instance
+ public static object GetServiceOrCreateInstance(IServiceProvider provider, Type type)
+ {
+ return provider.GetService(type) ?? CreateInstance(provider, type);
+ }
+
+ private static MethodInfo GetMethodInfo(Expression expr)
+ {
+ var mc = (MethodCallExpression)expr.Body;
+ return mc.Method;
+ }
+
+ private static object GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
+ {
+ var service = sp.GetService(type);
+ if (service == null && !isDefaultParameterRequired)
+ {
+ var message = $"Unable to resolve service for type '{type}' while attempting to activate '{requiredBy}'.";
+ throw new InvalidOperationException(message);
+ }
+ return service;
+ }
+
+ private static Expression BuildFactoryExpression(
+ ConstructorInfo constructor,
+ int?[] parameterMap,
+ Expression serviceProvider,
+ Expression factoryArgumentArray)
+ {
+ var constructorParameters = constructor.GetParameters();
+ var constructorArguments = new Expression[constructorParameters.Length];
+
+ for (var i = 0; i < constructorParameters.Length; i++)
+ {
+ var constructorParameter = constructorParameters[i];
+ var parameterType = constructorParameter.ParameterType;
+ var hasDefaultValue = ParameterDefaultValue.TryGetDefaultValue(constructorParameter, out var defaultValue);
+
+ if (parameterMap[i] != null)
+ {
+ constructorArguments[i] = Expression.ArrayAccess(factoryArgumentArray, Expression.Constant(parameterMap[i]));
+ }
+ else
+ {
+ var parameterTypeExpression = new Expression[] { serviceProvider,
+ Expression.Constant(parameterType, typeof(Type)),
+ Expression.Constant(constructor.DeclaringType, typeof(Type)),
+ Expression.Constant(hasDefaultValue) };
+ constructorArguments[i] = Expression.Call(GetServiceInfo, parameterTypeExpression);
+ }
+
+ // Support optional constructor arguments by passing in the default value
+ // when the argument would otherwise be null.
+ if (hasDefaultValue)
+ {
+ var defaultValueExpression = Expression.Constant(defaultValue);
+ constructorArguments[i] = Expression.Coalesce(constructorArguments[i], defaultValueExpression);
+ }
+
+ constructorArguments[i] = Expression.Convert(constructorArguments[i], parameterType);
+ }
+
+ return Expression.New(constructor, constructorArguments);
+ }
+
+ private static void FindApplicableConstructor(
+ Type instanceType,
+ Type[] argumentTypes,
+ out ConstructorInfo matchingConstructor,
+ out int?[] parameterMap)
+ {
+ matchingConstructor = null;
+ parameterMap = null;
+
+ if (!TryFindPreferredConstructor(instanceType, argumentTypes, ref matchingConstructor, ref parameterMap) &&
+ !TryFindMatchingConstructor(instanceType, argumentTypes, ref matchingConstructor, ref parameterMap))
+ {
+ var message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.";
+ throw new InvalidOperationException(message);
+ }
+ }
+
+ // Tries to find constructor based on provided argument types
+ private static bool TryFindMatchingConstructor(
+ Type instanceType,
+ Type[] argumentTypes,
+ ref ConstructorInfo matchingConstructor,
+ ref int?[] parameterMap)
+ {
+ foreach (var constructor in instanceType.GetTypeInfo().DeclaredConstructors)
+ {
+ if (constructor.IsStatic || !constructor.IsPublic)
+ {
+ continue;
+ }
+
+ if (TryCreateParameterMap(constructor.GetParameters(), argumentTypes, out int?[] tempParameterMap))
+ {
+ if (matchingConstructor != null)
+ {
+ throw new InvalidOperationException($"Multiple constructors accepting all given argument types have been found in type '{instanceType}'. There should only be one applicable constructor.");
+ }
+
+ matchingConstructor = constructor;
+ parameterMap = tempParameterMap;
+ }
+ }
+
+ return matchingConstructor != null;
+ }
+
+ // Tries to find constructor marked with ActivatorUtilitiesConstructorAttribute
+ private static bool TryFindPreferredConstructor(
+ Type instanceType,
+ Type[] argumentTypes,
+ ref ConstructorInfo matchingConstructor,
+ ref int?[] parameterMap)
+ {
+ var seenPreferred = false;
+ foreach (var constructor in instanceType.GetTypeInfo().DeclaredConstructors)
+ {
+ if (constructor.IsStatic || !constructor.IsPublic)
+ {
+ continue;
+ }
+
+ if (constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false))
+ {
+ if (seenPreferred)
+ {
+ ThrowMultipleCtorsMarkedWithAttributeException();
+ }
+
+ if (!TryCreateParameterMap(constructor.GetParameters(), argumentTypes, out int?[] tempParameterMap))
+ {
+ ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
+ }
+
+ matchingConstructor = constructor;
+ parameterMap = tempParameterMap;
+ seenPreferred = true;
+ }
+ }
+
+ return matchingConstructor != null;
+ }
+
+ // Creates an injective parameterMap from givenParameterTypes to assignable constructorParameters.
+ // Returns true if each given parameter type is assignable to a unique; otherwise, false.
+ private static bool TryCreateParameterMap(ParameterInfo[] constructorParameters, Type[] argumentTypes, out int?[] parameterMap)
+ {
+ parameterMap = new int?[constructorParameters.Length];
+
+ for (var i = 0; i < argumentTypes.Length; i++)
+ {
+ var foundMatch = false;
+ var givenParameter = argumentTypes[i].GetTypeInfo();
+
+ for (var j = 0; j < constructorParameters.Length; j++)
+ {
+ if (parameterMap[j] != null)
+ {
+ // This ctor parameter has already been matched
+ continue;
+ }
+
+ if (constructorParameters[j].ParameterType.GetTypeInfo().IsAssignableFrom(givenParameter))
+ {
+ foundMatch = true;
+ parameterMap[j] = i;
+ break;
+ }
+ }
+
+ if (!foundMatch)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private class ConstructorMatcher
+ {
+ private readonly ConstructorInfo _constructor;
+ private readonly ParameterInfo[] _parameters;
+ private readonly object[] _parameterValues;
+ private readonly bool[] _parameterValuesSet;
+
+ public ConstructorMatcher(ConstructorInfo constructor)
+ {
+ _constructor = constructor;
+ _parameters = _constructor.GetParameters();
+ _parameterValuesSet = new bool[_parameters.Length];
+ _parameterValues = new object[_parameters.Length];
+ }
+
+ public int Match(object[] givenParameters)
+ {
+ var applyIndexStart = 0;
+ var applyExactLength = 0;
+ for (var givenIndex = 0; givenIndex != givenParameters.Length; givenIndex++)
+ {
+ var givenType = givenParameters[givenIndex]?.GetType().GetTypeInfo();
+ var givenMatched = false;
+
+ for (var applyIndex = applyIndexStart; givenMatched == false && applyIndex != _parameters.Length; ++applyIndex)
+ {
+ if (_parameterValuesSet[applyIndex] == false &&
+ _parameters[applyIndex].ParameterType.GetTypeInfo().IsAssignableFrom(givenType))
+ {
+ givenMatched = true;
+ _parameterValuesSet[applyIndex] = true;
+ _parameterValues[applyIndex] = givenParameters[givenIndex];
+ if (applyIndexStart == applyIndex)
+ {
+ applyIndexStart++;
+ if (applyIndex == givenIndex)
+ {
+ applyExactLength = applyIndex;
+ }
+ }
+ }
+ }
+
+ if (givenMatched == false)
+ {
+ return -1;
+ }
+ }
+ return applyExactLength;
+ }
+
+ public object CreateInstance(IServiceProvider provider)
+ {
+ for (var index = 0; index != _parameters.Length; index++)
+ {
+ if (_parameterValuesSet[index] == false)
+ {
+ var value = provider.GetService(_parameters[index].ParameterType);
+ if (value == null)
+ {
+ if (!ParameterDefaultValue.TryGetDefaultValue(_parameters[index], out var defaultValue))
+ {
+ throw new InvalidOperationException($"Unable to resolve service for type '{_parameters[index].ParameterType}' while attempting to activate '{_constructor.DeclaringType}'.");
+ }
+ else
+ {
+ _parameterValues[index] = defaultValue;
+ }
+ }
+ else
+ {
+ _parameterValues[index] = value;
+ }
+ }
+ }
+
+ try
+ {
+ return _constructor.Invoke(_parameterValues);
+ }
+ catch (TargetInvocationException ex)
+ {
+ ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
+ // The above line will always throw, but the compiler requires we throw explicitly.
+ throw;
+ }
+ }
+ }
+
+ private static void ThrowMultipleCtorsMarkedWithAttributeException()
+ {
+ throw new InvalidOperationException($"Multiple constructors were marked with {nameof(ActivatorUtilitiesConstructorAttribute)}.");
+ }
+
+ private static void ThrowMarkedCtorDoesNotTakeAllProvidedArguments()
+ {
+ throw new InvalidOperationException($"Constructor marked with {nameof(ActivatorUtilitiesConstructorAttribute)} does not accept all given argument types.");
+ }
+ }
+}
diff --git a/src/libraries/Common/src/Extensions/ActivatorUtilities/ActivatorUtilitiesConstructorAttribute.cs b/src/libraries/Common/src/Extensions/ActivatorUtilities/ActivatorUtilitiesConstructorAttribute.cs
new file mode 100644
index 00000000000..67ffa13f6fc
--- /dev/null
+++ b/src/libraries/Common/src/Extensions/ActivatorUtilities/ActivatorUtilitiesConstructorAttribute.cs
@@ -0,0 +1,26 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+
+#if ActivatorUtilities_In_DependencyInjection
+namespace Microsoft.Extensions.DependencyInjection
+#else
+namespace Microsoft.Extensions.Internal
+#endif
+{
+ ///
+ /// Marks the constructor to be used when activating type using .
+ ///
+
+#if ActivatorUtilities_In_DependencyInjection
+ public
+#else
+ // Do not take a dependency on this class unless you are explicitly trying to avoid taking a
+ // dependency on Microsoft.AspNetCore.DependencyInjection.Abstractions.
+ internal
+#endif
+ class ActivatorUtilitiesConstructorAttribute: Attribute
+ {
+ }
+}
diff --git a/src/libraries/Common/src/Extensions/ActivatorUtilities/ObjectFactory.cs b/src/libraries/Common/src/Extensions/ActivatorUtilities/ObjectFactory.cs
new file mode 100644
index 00000000000..517247811ec
--- /dev/null
+++ b/src/libraries/Common/src/Extensions/ActivatorUtilities/ObjectFactory.cs
@@ -0,0 +1,25 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+
+#if ActivatorUtilities_In_DependencyInjection
+namespace Microsoft.Extensions.DependencyInjection
+#else
+namespace Microsoft.Extensions.Internal
+#endif
+{
+
+ ///
+ /// The result of .
+ ///
+ /// The to get service arguments from.
+ /// Additional constructor arguments.
+ /// The instantiated type.
+#if ActivatorUtilities_In_DependencyInjection
+ public
+#else
+ internal
+#endif
+ delegate object ObjectFactory(IServiceProvider serviceProvider, object[] arguments);
+}
\ No newline at end of file
diff --git a/src/libraries/Common/src/Extensions/ActivatorUtilities/sharedsources.props b/src/libraries/Common/src/Extensions/ActivatorUtilities/sharedsources.props
new file mode 100644
index 00000000000..b35fe34b10f
--- /dev/null
+++ b/src/libraries/Common/src/Extensions/ActivatorUtilities/sharedsources.props
@@ -0,0 +1,8 @@
+
+
+
+ true
+ $(ContentTargetFolders)\cs\netstandard1.0\
+
+
+
diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs
new file mode 100644
index 00000000000..a71bad37b1a
--- /dev/null
+++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs
@@ -0,0 +1,47 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Reflection;
+
+namespace Microsoft.Extensions.Internal
+{
+ internal class ParameterDefaultValue
+ {
+ public static bool TryGetDefaultValue(ParameterInfo parameter, out object defaultValue)
+ {
+ bool hasDefaultValue;
+ var tryToGetDefaultValue = true;
+ defaultValue = null;
+
+ try
+ {
+ hasDefaultValue = parameter.HasDefaultValue;
+ }
+ catch (FormatException) when (parameter.ParameterType == typeof(DateTime))
+ {
+ // Workaround for https://github.com/dotnet/corefx/issues/12338
+ // If HasDefaultValue throws FormatException for DateTime
+ // we expect it to have default value
+ hasDefaultValue = true;
+ tryToGetDefaultValue = false;
+ }
+
+ if (hasDefaultValue)
+ {
+ if (tryToGetDefaultValue)
+ {
+ defaultValue = parameter.DefaultValue;
+ }
+
+ // Workaround for https://github.com/dotnet/corefx/issues/11797
+ if (defaultValue == null && parameter.ParameterType.IsValueType)
+ {
+ defaultValue = Activator.CreateInstance(parameter.ParameterType);
+ }
+ }
+
+ return hasDefaultValue;
+ }
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.cs
new file mode 100644
index 00000000000..300ded03ce5
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.cs
@@ -0,0 +1,722 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Extensions.DependencyInjection.Abstractions;
+
+namespace Microsoft.Extensions.DependencyInjection.Extensions
+{
+ ///
+ /// Extension methods for adding and removing services to an .
+ ///
+ public static class ServiceCollectionDescriptorExtensions
+ {
+ ///
+ /// Adds the specified to the .
+ ///
+ /// The .
+ /// The to add.
+ /// A reference to the current instance of .
+ public static IServiceCollection Add(
+ this IServiceCollection collection,
+ ServiceDescriptor descriptor)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (descriptor == null)
+ {
+ throw new ArgumentNullException(nameof(descriptor));
+ }
+
+ collection.Add(descriptor);
+ return collection;
+ }
+
+ ///
+ /// Adds a sequence of to the .
+ ///
+ /// The .
+ /// The s to add.
+ /// A reference to the current instance of .
+ public static IServiceCollection Add(
+ this IServiceCollection collection,
+ IEnumerable descriptors)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (descriptors == null)
+ {
+ throw new ArgumentNullException(nameof(descriptors));
+ }
+
+ foreach (var descriptor in descriptors)
+ {
+ collection.Add(descriptor);
+ }
+
+ return collection;
+ }
+
+ ///
+ /// Adds the specified to the if the
+ /// service type hasn't already been registered.
+ ///
+ /// The .
+ /// The to add.
+ public static void TryAdd(
+ this IServiceCollection collection,
+ ServiceDescriptor descriptor)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (descriptor == null)
+ {
+ throw new ArgumentNullException(nameof(descriptor));
+ }
+
+ if (!collection.Any(d => d.ServiceType == descriptor.ServiceType))
+ {
+ collection.Add(descriptor);
+ }
+ }
+
+ ///
+ /// Adds the specified to the if the
+ /// service type hasn't already been registered.
+ ///
+ /// The .
+ /// The s to add.
+ public static void TryAdd(
+ this IServiceCollection collection,
+ IEnumerable descriptors)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (descriptors == null)
+ {
+ throw new ArgumentNullException(nameof(descriptors));
+ }
+
+ foreach (var d in descriptors)
+ {
+ collection.TryAdd(d);
+ }
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ public static void TryAddTransient(
+ this IServiceCollection collection,
+ Type service)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ var descriptor = ServiceDescriptor.Transient(service, service);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// with the implementation
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ /// The implementation type of the service.
+ public static void TryAddTransient(
+ this IServiceCollection collection,
+ Type service,
+ Type implementationType)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ var descriptor = ServiceDescriptor.Transient(service, implementationType);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// using the factory specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ /// The factory that creates the service.
+ public static void TryAddTransient(
+ this IServiceCollection collection,
+ Type service,
+ Func implementationFactory)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ var descriptor = ServiceDescriptor.Transient(service, implementationFactory);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The .
+ public static void TryAddTransient(this IServiceCollection collection)
+ where TService : class
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ TryAddTransient(collection, typeof(TService), typeof(TService));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// implementation type specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The .
+ public static void TryAddTransient(this IServiceCollection collection)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ TryAddTransient(collection, typeof(TService), typeof(TImplementation));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// using the factory specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The .
+ /// The factory that creates the service.
+ public static void TryAddTransient(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ {
+ services.TryAdd(ServiceDescriptor.Transient(implementationFactory));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ public static void TryAddScoped(
+ this IServiceCollection collection,
+ Type service)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ var descriptor = ServiceDescriptor.Scoped(service, service);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// with the implementation
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ /// The implementation type of the service.
+ public static void TryAddScoped(
+ this IServiceCollection collection,
+ Type service,
+ Type implementationType)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ var descriptor = ServiceDescriptor.Scoped(service, implementationType);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// using the factory specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ /// The factory that creates the service.
+ public static void TryAddScoped(
+ this IServiceCollection collection,
+ Type service,
+ Func implementationFactory)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ var descriptor = ServiceDescriptor.Scoped(service, implementationFactory);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The .
+ public static void TryAddScoped(this IServiceCollection collection)
+ where TService : class
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ TryAddScoped(collection, typeof(TService), typeof(TService));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// implementation type specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The .
+ public static void TryAddScoped(this IServiceCollection collection)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ TryAddScoped(collection, typeof(TService), typeof(TImplementation));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// using the factory specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The .
+ /// The factory that creates the service.
+ public static void TryAddScoped(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ {
+ services.TryAdd(ServiceDescriptor.Scoped(implementationFactory));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ public static void TryAddSingleton(
+ this IServiceCollection collection,
+ Type service)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ var descriptor = ServiceDescriptor.Singleton(service, service);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// with the implementation
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ /// The implementation type of the service.
+ public static void TryAddSingleton(
+ this IServiceCollection collection,
+ Type service,
+ Type implementationType)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ var descriptor = ServiceDescriptor.Singleton(service, implementationType);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// using the factory specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The .
+ /// The type of the service to register.
+ /// The factory that creates the service.
+ public static void TryAddSingleton(
+ this IServiceCollection collection,
+ Type service,
+ Func implementationFactory)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ var descriptor = ServiceDescriptor.Singleton(service, implementationFactory);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The .
+ public static void TryAddSingleton(this IServiceCollection collection)
+ where TService : class
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ TryAddSingleton(collection, typeof(TService), typeof(TService));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// implementation type specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The .
+ public static void TryAddSingleton(this IServiceCollection collection)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ TryAddSingleton(collection, typeof(TService), typeof(TImplementation));
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// with an instance specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The .
+ /// The instance of the service to add.
+ public static void TryAddSingleton(this IServiceCollection collection, TService instance)
+ where TService : class
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (instance == null)
+ {
+ throw new ArgumentNullException(nameof(instance));
+ }
+
+ var descriptor = ServiceDescriptor.Singleton(typeof(TService), instance);
+ TryAdd(collection, descriptor);
+ }
+
+ ///
+ /// Adds the specified as a service
+ /// using the factory specified in
+ /// to the if the service type hasn't already been registered.
+ ///
+ /// The type of the service to add.
+ /// The .
+ /// The factory that creates the service.
+ public static void TryAddSingleton(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ {
+ services.TryAdd(ServiceDescriptor.Singleton(implementationFactory));
+ }
+
+ ///
+ /// Adds a if an existing descriptor with the same
+ /// and an implementation that does not already exist
+ /// in .
+ ///
+ /// The .
+ /// The .
+ ///
+ /// Use when registing a service implementation of a
+ /// service type that
+ /// supports multiple registrations of the same service type. Using
+ /// is not idempotent and can add
+ /// duplicate
+ /// instances if called twice. Using
+ /// will prevent registration
+ /// of multiple implementation types.
+ ///
+ public static void TryAddEnumerable(
+ this IServiceCollection services,
+ ServiceDescriptor descriptor)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (descriptor == null)
+ {
+ throw new ArgumentNullException(nameof(descriptor));
+ }
+
+ var implementationType = descriptor.GetImplementationType();
+
+ if (implementationType == typeof(object) ||
+ implementationType == descriptor.ServiceType)
+ {
+ throw new ArgumentException(
+ Resources.FormatTryAddIndistinguishableTypeToEnumerable(
+ implementationType,
+ descriptor.ServiceType),
+ nameof(descriptor));
+ }
+
+ if (!services.Any(d =>
+ d.ServiceType == descriptor.ServiceType &&
+ d.GetImplementationType() == implementationType))
+ {
+ services.Add(descriptor);
+ }
+ }
+
+ ///
+ /// Adds the specified s if an existing descriptor with the same
+ /// and an implementation that does not already exist
+ /// in .
+ ///
+ /// The .
+ /// The s.
+ ///
+ /// Use when registing a service
+ /// implementation of a service type that
+ /// supports multiple registrations of the same service type. Using
+ /// is not idempotent and can add
+ /// duplicate
+ /// instances if called twice. Using
+ /// will prevent registration
+ /// of multiple implementation types.
+ ///
+ public static void TryAddEnumerable(
+ this IServiceCollection services,
+ IEnumerable descriptors)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (descriptors == null)
+ {
+ throw new ArgumentNullException(nameof(descriptors));
+ }
+
+ foreach (var d in descriptors)
+ {
+ services.TryAddEnumerable(d);
+ }
+ }
+
+ ///
+ /// Removes the first service in with the same service type
+ /// as and adds to the collection.
+ ///
+ /// The .
+ /// The to replace with.
+ ///
+ public static IServiceCollection Replace(
+ this IServiceCollection collection,
+ ServiceDescriptor descriptor)
+ {
+ if (collection == null)
+ {
+ throw new ArgumentNullException(nameof(collection));
+ }
+
+ if (descriptor == null)
+ {
+ throw new ArgumentNullException(nameof(descriptor));
+ }
+
+ var registeredServiceDescriptor = collection.FirstOrDefault(s => s.ServiceType == descriptor.ServiceType);
+ if (registeredServiceDescriptor != null)
+ {
+ collection.Remove(registeredServiceDescriptor);
+ }
+
+ collection.Add(descriptor);
+ return collection;
+ }
+
+ ///
+ /// Removes all services of type in .
+ ///
+ /// The .
+ ///
+ public static IServiceCollection RemoveAll(this IServiceCollection collection)
+ {
+ return RemoveAll(collection, typeof(T));
+ }
+
+ ///
+ /// Removes all services of type in .
+ ///
+ /// The .
+ /// The service type to remove.
+ ///
+ public static IServiceCollection RemoveAll(this IServiceCollection collection, Type serviceType)
+ {
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ for (var i = collection.Count - 1; i >= 0; i--)
+ {
+ var descriptor = collection[i];
+ if (descriptor.ServiceType == serviceType)
+ {
+ collection.RemoveAt(i);
+ }
+ }
+
+ return collection;
+ }
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceCollection.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceCollection.cs
new file mode 100644
index 00000000000..6caf01d3d88
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceCollection.cs
@@ -0,0 +1,14 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System.Collections.Generic;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// Specifies the contract for a collection of service descriptors.
+ ///
+ public interface IServiceCollection : IList
+ {
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceProviderFactory.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceProviderFactory.cs
new file mode 100644
index 00000000000..ad01acdc7e2
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceProviderFactory.cs
@@ -0,0 +1,24 @@
+using System;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// Provides an extension point for creating a container specific builder and an .
+ ///
+ public interface IServiceProviderFactory
+ {
+ ///
+ /// Creates a container builder from an .
+ ///
+ /// The collection of services
+ /// A container builder that can be used to create an .
+ TContainerBuilder CreateBuilder(IServiceCollection services);
+
+ ///
+ /// Creates an from the container builder.
+ ///
+ /// The container builder
+ /// An
+ IServiceProvider CreateServiceProvider(TContainerBuilder containerBuilder);
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceScope.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceScope.cs
new file mode 100644
index 00000000000..4a6ee34be06
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceScope.cs
@@ -0,0 +1,21 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// The method ends the scope lifetime. Once Dispose
+ /// is called, any scoped services that have been resolved from
+ /// will be
+ /// disposed.
+ ///
+ public interface IServiceScope : IDisposable
+ {
+ ///
+ /// The used to resolve dependencies from the scope.
+ ///
+ IServiceProvider ServiceProvider { get; }
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceScopeFactory.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceScopeFactory.cs
new file mode 100644
index 00000000000..0ec6ccd9313
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IServiceScopeFactory.cs
@@ -0,0 +1,25 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// A factory for creating instances of , which is used to create
+ /// services within a scope.
+ ///
+ public interface IServiceScopeFactory
+ {
+ ///
+ /// Create an which
+ /// contains an used to resolve dependencies from a
+ /// newly created scope.
+ ///
+ ///
+ /// An controlling the
+ /// lifetime of the scope. Once this is disposed, any scoped services that have been resolved
+ /// from the
+ /// will also be disposed.
+ ///
+ IServiceScope CreateScope();
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ISupportRequiredService.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ISupportRequiredService.cs
new file mode 100644
index 00000000000..3944d744555
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ISupportRequiredService.cs
@@ -0,0 +1,23 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// Optional contract used by
+ /// to resolve services if supported by .
+ ///
+ public interface ISupportRequiredService
+ {
+ ///
+ /// Gets service of type from the implementing
+ /// this interface.
+ ///
+ /// An object that specifies the type of service object to get.
+ /// A service object of type .
+ /// Throws an exception if the cannot create the object.
+ object GetRequiredService(Type serviceType);
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj
new file mode 100644
index 00000000000..6f6ae0355b5
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj
@@ -0,0 +1,18 @@
+
+
+
+ Abstractions for dependency injection.
+Commonly used types:
+Microsoft.Extensions.DependencyInjection.IServiceCollection
+ netstandard2.0
+ true
+ dependencyinjection;di
+ $(DefineConstants);ActivatorUtilities_In_DependencyInjection
+
+
+
+
+
+
+
+
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Properties/AssemblyInfo.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Properties/AssemblyInfo.cs
new file mode 100644
index 00000000000..7586e5bfdad
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Properties/AssemblyInfo.cs
@@ -0,0 +1,6 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System.Runtime.CompilerServices;
+
+[assembly: InternalsVisibleTo("Microsoft.Extensions.DependencyInjection.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Properties/Resources.Designer.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Properties/Resources.Designer.cs
new file mode 100644
index 00000000000..63275d99823
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Properties/Resources.Designer.cs
@@ -0,0 +1,114 @@
+//
+namespace Microsoft.Extensions.DependencyInjection.Abstractions
+{
+ using System.Globalization;
+ using System.Reflection;
+ using System.Resources;
+
+ internal static class Resources
+ {
+ private static readonly ResourceManager _resourceManager
+ = new ResourceManager("Microsoft.Extensions.DependencyInjection.Abstractions.Resources", typeof(Resources).GetTypeInfo().Assembly);
+
+ ///
+ /// Multiple constructors accepting all given argument types have been found in type '{0}'. There should only be one applicable constructor.
+ ///
+ internal static string AmbiguousConstructorMatch
+ {
+ get => GetString("AmbiguousConstructorMatch");
+ }
+
+ ///
+ /// Multiple constructors accepting all given argument types have been found in type '{0}'. There should only be one applicable constructor.
+ ///
+ internal static string FormatAmbiguousConstructorMatch(object p0)
+ => string.Format(CultureInfo.CurrentCulture, GetString("AmbiguousConstructorMatch"), p0);
+
+ ///
+ /// Unable to locate implementation '{0}' for service '{1}'.
+ ///
+ internal static string CannotLocateImplementation
+ {
+ get => GetString("CannotLocateImplementation");
+ }
+
+ ///
+ /// Unable to locate implementation '{0}' for service '{1}'.
+ ///
+ internal static string FormatCannotLocateImplementation(object p0, object p1)
+ => string.Format(CultureInfo.CurrentCulture, GetString("CannotLocateImplementation"), p0, p1);
+
+ ///
+ /// Unable to resolve service for type '{0}' while attempting to activate '{1}'.
+ ///
+ internal static string CannotResolveService
+ {
+ get => GetString("CannotResolveService");
+ }
+
+ ///
+ /// Unable to resolve service for type '{0}' while attempting to activate '{1}'.
+ ///
+ internal static string FormatCannotResolveService(object p0, object p1)
+ => string.Format(CultureInfo.CurrentCulture, GetString("CannotResolveService"), p0, p1);
+
+ ///
+ /// A suitable constructor for type '{0}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
+ ///
+ internal static string NoConstructorMatch
+ {
+ get => GetString("NoConstructorMatch");
+ }
+
+ ///
+ /// A suitable constructor for type '{0}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
+ ///
+ internal static string FormatNoConstructorMatch(object p0)
+ => string.Format(CultureInfo.CurrentCulture, GetString("NoConstructorMatch"), p0);
+
+ ///
+ /// No service for type '{0}' has been registered.
+ ///
+ internal static string NoServiceRegistered
+ {
+ get => GetString("NoServiceRegistered");
+ }
+
+ ///
+ /// No service for type '{0}' has been registered.
+ ///
+ internal static string FormatNoServiceRegistered(object p0)
+ => string.Format(CultureInfo.CurrentCulture, GetString("NoServiceRegistered"), p0);
+
+ ///
+ /// Implementation type cannot be '{0}' because it is indistinguishable from other services registered for '{1}'.
+ ///
+ internal static string TryAddIndistinguishableTypeToEnumerable
+ {
+ get => GetString("TryAddIndistinguishableTypeToEnumerable");
+ }
+
+ ///
+ /// Implementation type cannot be '{0}' because it is indistinguishable from other services registered for '{1}'.
+ ///
+ internal static string FormatTryAddIndistinguishableTypeToEnumerable(object p0, object p1)
+ => string.Format(CultureInfo.CurrentCulture, GetString("TryAddIndistinguishableTypeToEnumerable"), p0, p1);
+
+ private static string GetString(string name, params string[] formatterNames)
+ {
+ var value = _resourceManager.GetString(name);
+
+ System.Diagnostics.Debug.Assert(value != null);
+
+ if (formatterNames != null)
+ {
+ for (var i = 0; i < formatterNames.Length; i++)
+ {
+ value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}");
+ }
+ }
+
+ return value;
+ }
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Resources.resx b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Resources.resx
new file mode 100644
index 00000000000..9f74883c8e8
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Resources.resx
@@ -0,0 +1,144 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ Multiple constructors accepting all given argument types have been found in type '{0}'. There should only be one applicable constructor.
+ {0} = service type
+
+
+ Unable to locate implementation '{0}' for service '{1}'.
+ {0} = implementation type, {1} = service type
+
+
+ Unable to resolve service for type '{0}' while attempting to activate '{1}'.
+ {0} = service which cannot be resolved, {1} = service being activated
+
+
+ A suitable constructor for type '{0}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
+ {0} = service type
+
+
+ No service for type '{0}' has been registered.
+ {0} = service type
+
+
+ Implementation type cannot be '{0}' because it is indistinguishable from other services registered for '{1}'.
+ {0} = implementation type, {1} = service type
+
+
\ No newline at end of file
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceCollectionServiceExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceCollectionServiceExtensions.cs
new file mode 100644
index 00000000000..a000b0c32ce
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceCollectionServiceExtensions.cs
@@ -0,0 +1,673 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// Extension methods for adding services to an .
+ ///
+ public static class ServiceCollectionServiceExtensions
+ {
+ ///
+ /// Adds a transient service of the type specified in with an
+ /// implementation of the type specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register.
+ /// The implementation type of the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddTransient(
+ this IServiceCollection services,
+ Type serviceType,
+ Type implementationType)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ return Add(services, serviceType, implementationType, ServiceLifetime.Transient);
+ }
+
+ ///
+ /// Adds a transient service of the type specified in with a
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddTransient(
+ this IServiceCollection services,
+ Type serviceType,
+ Func implementationFactory)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Add(services, serviceType, implementationFactory, ServiceLifetime.Transient);
+ }
+
+ ///
+ /// Adds a transient service of the type specified in with an
+ /// implementation type specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The to add the service to.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddTransient(this IServiceCollection services)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ return services.AddTransient(typeof(TService), typeof(TImplementation));
+ }
+
+ ///
+ /// Adds a transient service of the type specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register and the implementation to use.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddTransient(
+ this IServiceCollection services,
+ Type serviceType)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ return services.AddTransient(serviceType, serviceType);
+ }
+
+ ///
+ /// Adds a transient service of the type specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The to add the service to.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddTransient(this IServiceCollection services)
+ where TService : class
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ return services.AddTransient(typeof(TService));
+ }
+
+ ///
+ /// Adds a transient service of the type specified in with a
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The to add the service to.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddTransient(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return services.AddTransient(typeof(TService), implementationFactory);
+ }
+
+ ///
+ /// Adds a transient service of the type specified in with an
+ /// implementation type specified in using the
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The to add the service to.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddTransient(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return services.AddTransient(typeof(TService), implementationFactory);
+ }
+
+
+
+ ///
+ /// Adds a scoped service of the type specified in with an
+ /// implementation of the type specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register.
+ /// The implementation type of the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddScoped(
+ this IServiceCollection services,
+ Type serviceType,
+ Type implementationType)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ return Add(services, serviceType, implementationType, ServiceLifetime.Scoped);
+ }
+
+ ///
+ /// Adds a scoped service of the type specified in with a
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddScoped(
+ this IServiceCollection services,
+ Type serviceType,
+ Func implementationFactory)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Add(services, serviceType, implementationFactory, ServiceLifetime.Scoped);
+ }
+
+ ///
+ /// Adds a scoped service of the type specified in with an
+ /// implementation type specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The to add the service to.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddScoped(this IServiceCollection services)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ return services.AddScoped(typeof(TService), typeof(TImplementation));
+ }
+
+ ///
+ /// Adds a scoped service of the type specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register and the implementation to use.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddScoped(
+ this IServiceCollection services,
+ Type serviceType)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ return services.AddScoped(serviceType, serviceType);
+ }
+
+ ///
+ /// Adds a scoped service of the type specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The to add the service to.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddScoped(this IServiceCollection services)
+ where TService : class
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ return services.AddScoped(typeof(TService));
+ }
+
+ ///
+ /// Adds a scoped service of the type specified in with a
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The to add the service to.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddScoped(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return services.AddScoped(typeof(TService), implementationFactory);
+ }
+
+ ///
+ /// Adds a scoped service of the type specified in with an
+ /// implementation type specified in using the
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The to add the service to.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddScoped(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return services.AddScoped(typeof(TService), implementationFactory);
+ }
+
+
+ ///
+ /// Adds a singleton service of the type specified in with an
+ /// implementation of the type specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register.
+ /// The implementation type of the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(
+ this IServiceCollection services,
+ Type serviceType,
+ Type implementationType)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ return Add(services, serviceType, implementationType, ServiceLifetime.Singleton);
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in with a
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(
+ this IServiceCollection services,
+ Type serviceType,
+ Func implementationFactory)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Add(services, serviceType, implementationFactory, ServiceLifetime.Singleton);
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in with an
+ /// implementation type specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The to add the service to.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(this IServiceCollection services)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ return services.AddSingleton(typeof(TService), typeof(TImplementation));
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register and the implementation to use.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(
+ this IServiceCollection services,
+ Type serviceType)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ return services.AddSingleton(serviceType, serviceType);
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The to add the service to.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(this IServiceCollection services)
+ where TService : class
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ return services.AddSingleton(typeof(TService));
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in with a
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The to add the service to.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return services.AddSingleton(typeof(TService), implementationFactory);
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in with an
+ /// implementation type specified in using the
+ /// factory specified in to the
+ /// specified .
+ ///
+ /// The type of the service to add.
+ /// The type of the implementation to use.
+ /// The to add the service to.
+ /// The factory that creates the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(
+ this IServiceCollection services,
+ Func implementationFactory)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return services.AddSingleton(typeof(TService), implementationFactory);
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in with an
+ /// instance specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The type of the service to register.
+ /// The instance of the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(
+ this IServiceCollection services,
+ Type serviceType,
+ object implementationInstance)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationInstance == null)
+ {
+ throw new ArgumentNullException(nameof(implementationInstance));
+ }
+
+ var serviceDescriptor = new ServiceDescriptor(serviceType, implementationInstance);
+ services.Add(serviceDescriptor);
+ return services;
+ }
+
+ ///
+ /// Adds a singleton service of the type specified in with an
+ /// instance specified in to the
+ /// specified .
+ ///
+ /// The to add the service to.
+ /// The instance of the service.
+ /// A reference to this instance after the operation has completed.
+ ///
+ public static IServiceCollection AddSingleton(
+ this IServiceCollection services,
+ TService implementationInstance)
+ where TService : class
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+
+ if (implementationInstance == null)
+ {
+ throw new ArgumentNullException(nameof(implementationInstance));
+ }
+
+ return services.AddSingleton(typeof(TService), implementationInstance);
+ }
+
+ private static IServiceCollection Add(
+ IServiceCollection collection,
+ Type serviceType,
+ Type implementationType,
+ ServiceLifetime lifetime)
+ {
+ var descriptor = new ServiceDescriptor(serviceType, implementationType, lifetime);
+ collection.Add(descriptor);
+ return collection;
+ }
+
+ private static IServiceCollection Add(
+ IServiceCollection collection,
+ Type serviceType,
+ Func implementationFactory,
+ ServiceLifetime lifetime)
+ {
+ var descriptor = new ServiceDescriptor(serviceType, implementationFactory, lifetime);
+ collection.Add(descriptor);
+ return collection;
+ }
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs
new file mode 100644
index 00000000000..5b1765756b6
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs
@@ -0,0 +1,516 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Diagnostics;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// Describes a service with its service type, implementation, and lifetime.
+ ///
+ [DebuggerDisplay("Lifetime = {Lifetime}, ServiceType = {ServiceType}, ImplementationType = {ImplementationType}")]
+ public class ServiceDescriptor
+ {
+ ///
+ /// Initializes a new instance of with the specified .
+ ///
+ /// The of the service.
+ /// The implementing the service.
+ /// The of the service.
+ public ServiceDescriptor(
+ Type serviceType,
+ Type implementationType,
+ ServiceLifetime lifetime)
+ : this(serviceType, lifetime)
+ {
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ ImplementationType = implementationType;
+ }
+
+ ///
+ /// Initializes a new instance of with the specified
+ /// as a .
+ ///
+ /// The of the service.
+ /// The instance implementing the service.
+ public ServiceDescriptor(
+ Type serviceType,
+ object instance)
+ : this(serviceType, ServiceLifetime.Singleton)
+ {
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (instance == null)
+ {
+ throw new ArgumentNullException(nameof(instance));
+ }
+
+ ImplementationInstance = instance;
+ }
+
+ ///
+ /// Initializes a new instance of with the specified .
+ ///
+ /// The of the service.
+ /// A factory used for creating service instances.
+ /// The of the service.
+ public ServiceDescriptor(
+ Type serviceType,
+ Func factory,
+ ServiceLifetime lifetime)
+ : this(serviceType, lifetime)
+ {
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (factory == null)
+ {
+ throw new ArgumentNullException(nameof(factory));
+ }
+
+ ImplementationFactory = factory;
+ }
+
+ private ServiceDescriptor(Type serviceType, ServiceLifetime lifetime)
+ {
+ Lifetime = lifetime;
+ ServiceType = serviceType;
+ }
+
+ ///
+ public ServiceLifetime Lifetime { get; }
+
+ ///
+ public Type ServiceType { get; }
+
+ ///
+ public Type ImplementationType { get; }
+
+ ///
+ public object ImplementationInstance { get; }
+
+ ///
+ public Func ImplementationFactory { get; }
+
+ internal Type GetImplementationType()
+ {
+ if (ImplementationType != null)
+ {
+ return ImplementationType;
+ }
+ else if (ImplementationInstance != null)
+ {
+ return ImplementationInstance.GetType();
+ }
+ else if (ImplementationFactory != null)
+ {
+ var typeArguments = ImplementationFactory.GetType().GenericTypeArguments;
+
+ Debug.Assert(typeArguments.Length == 2);
+
+ return typeArguments[1];
+ }
+
+ Debug.Assert(false, "ImplementationType, ImplementationInstance or ImplementationFactory must be non null");
+ return null;
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Transient()
+ where TService : class
+ where TImplementation : class, TService
+ {
+ return Describe(ServiceLifetime.Transient);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// and
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Transient(Type service, Type implementationType)
+ {
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ return Describe(service, implementationType, ServiceLifetime.Transient);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Transient(
+ Func implementationFactory)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(typeof(TService), implementationFactory, ServiceLifetime.Transient);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Transient(Func implementationFactory)
+ where TService : class
+ {
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(typeof(TService), implementationFactory, ServiceLifetime.Transient);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Transient(Type service, Func implementationFactory)
+ {
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(service, implementationFactory, ServiceLifetime.Transient);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Scoped()
+ where TService : class
+ where TImplementation : class, TService
+ {
+ return Describe(ServiceLifetime.Scoped);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// and
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Scoped(Type service, Type implementationType)
+ {
+ return Describe(service, implementationType, ServiceLifetime.Scoped);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Scoped(
+ Func implementationFactory)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(typeof(TService), implementationFactory, ServiceLifetime.Scoped);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Scoped(Func implementationFactory)
+ where TService : class
+ {
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(typeof(TService), implementationFactory, ServiceLifetime.Scoped);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Scoped(Type service, Func implementationFactory)
+ {
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(service, implementationFactory, ServiceLifetime.Scoped);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Singleton()
+ where TService : class
+ where TImplementation : class, TService
+ {
+ return Describe(ServiceLifetime.Singleton);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// and
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Singleton(Type service, Type implementationType)
+ {
+ if (service == null)
+ {
+ throw new ArgumentNullException(nameof(service));
+ }
+
+ if (implementationType == null)
+ {
+ throw new ArgumentNullException(nameof(implementationType));
+ }
+
+ return Describe(service, implementationType, ServiceLifetime.Singleton);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Singleton(
+ Func implementationFactory)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(typeof(TService), implementationFactory, ServiceLifetime.Singleton);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Singleton(Func implementationFactory)
+ where TService : class
+ {
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(typeof(TService), implementationFactory, ServiceLifetime.Singleton);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// A factory to create new instances of the service implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Singleton(
+ Type serviceType,
+ Func implementationFactory)
+ {
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationFactory == null)
+ {
+ throw new ArgumentNullException(nameof(implementationFactory));
+ }
+
+ return Describe(serviceType, implementationFactory, ServiceLifetime.Singleton);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The instance of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Singleton(TService implementationInstance)
+ where TService : class
+ {
+ if (implementationInstance == null)
+ {
+ throw new ArgumentNullException(nameof(implementationInstance));
+ }
+
+ return Singleton(typeof(TService), implementationInstance);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and the lifetime.
+ ///
+ /// The type of the service.
+ /// The instance of the implementation.
+ /// A new instance of .
+ public static ServiceDescriptor Singleton(
+ Type serviceType,
+ object implementationInstance)
+ {
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ if (implementationInstance == null)
+ {
+ throw new ArgumentNullException(nameof(implementationInstance));
+ }
+
+ return new ServiceDescriptor(serviceType, implementationInstance);
+ }
+
+ private static ServiceDescriptor Describe(ServiceLifetime lifetime)
+ where TService : class
+ where TImplementation : class, TService
+ {
+ return Describe(
+ typeof(TService),
+ typeof(TImplementation),
+ lifetime: lifetime);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and .
+ ///
+ /// The type of the service.
+ /// The type of the implementation.
+ /// The lifetime of the service.
+ /// A new instance of .
+ public static ServiceDescriptor Describe(Type serviceType, Type implementationType, ServiceLifetime lifetime)
+ {
+ return new ServiceDescriptor(serviceType, implementationType, lifetime);
+ }
+
+ ///
+ /// Creates an instance of with the specified
+ /// , ,
+ /// and .
+ ///
+ /// The type of the service.
+ /// A factory to create new instances of the service implementation.
+ /// The lifetime of the service.
+ /// A new instance of .
+ public static ServiceDescriptor Describe(Type serviceType, Func implementationFactory, ServiceLifetime lifetime)
+ {
+ return new ServiceDescriptor(serviceType, implementationFactory, lifetime);
+ }
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceLifetime.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceLifetime.cs
new file mode 100644
index 00000000000..8606d578048
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceLifetime.cs
@@ -0,0 +1,27 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// Specifies the lifetime of a service in an .
+ ///
+ public enum ServiceLifetime
+ {
+ ///
+ /// Specifies that a single instance of the service will be created.
+ ///
+ Singleton,
+ ///
+ /// Specifies that a new instance of the service will be created for each scope.
+ ///
+ ///
+ /// In ASP.NET Core applications a scope is created around each server request.
+ ///
+ Scoped,
+ ///
+ /// Specifies that a new instance of the service will be created every time it is requested.
+ ///
+ Transient
+ }
+}
\ No newline at end of file
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceProviderServiceExtensions.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceProviderServiceExtensions.cs
new file mode 100644
index 00000000000..86a0125a0d9
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceProviderServiceExtensions.cs
@@ -0,0 +1,130 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Collections.Generic;
+using Microsoft.Extensions.DependencyInjection.Abstractions;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// Extension methods for getting services from an .
+ ///
+ public static class ServiceProviderServiceExtensions
+ {
+ ///
+ /// Get service of type from the .
+ ///
+ /// The type of service object to get.
+ /// The to retrieve the service object from.
+ /// A service object of type or null if there is no such service.
+ public static T GetService(this IServiceProvider provider)
+ {
+ if (provider == null)
+ {
+ throw new ArgumentNullException(nameof(provider));
+ }
+
+ return (T)provider.GetService(typeof(T));
+ }
+
+ ///
+ /// Get service of type from the .
+ ///
+ /// The to retrieve the service object from.
+ /// An object that specifies the type of service object to get.
+ /// A service object of type .
+ /// There is no service of type .
+ public static object GetRequiredService(this IServiceProvider provider, Type serviceType)
+ {
+ if (provider == null)
+ {
+ throw new ArgumentNullException(nameof(provider));
+ }
+
+ if (serviceType == null)
+ {
+ throw new ArgumentNullException(nameof(serviceType));
+ }
+
+ var requiredServiceSupportingProvider = provider as ISupportRequiredService;
+ if (requiredServiceSupportingProvider != null)
+ {
+ return requiredServiceSupportingProvider.GetRequiredService(serviceType);
+ }
+
+ var service = provider.GetService(serviceType);
+ if (service == null)
+ {
+ throw new InvalidOperationException(Resources.FormatNoServiceRegistered(serviceType));
+ }
+
+ return service;
+ }
+
+ ///
+ /// Get service of type from the .
+ ///
+ /// The type of service object to get.
+ /// The to retrieve the service object from.
+ /// A service object of type .
+ /// There is no service of type .
+ public static T GetRequiredService(this IServiceProvider provider)
+ {
+ if (provider == null)
+ {
+ throw new ArgumentNullException(nameof(provider));
+ }
+
+ return (T)provider.GetRequiredService(typeof(T));
+ }
+
+ ///
+ /// Get an enumeration of services of type from the .
+ ///
+ /// The type of service object to get.
+ /// The to retrieve the services from.
+ /// An enumeration of services of type .
+ public static IEnumerable GetServices(this IServiceProvider provider)
+ {
+ if (provider == null)
+ {
+ throw new ArgumentNullException(nameof(provider));
+ }
+
+ return provider.GetRequiredService>();
+ }
+
+ ///
+ /// Get an enumeration of services of type from the .
+ ///
+ /// The to retrieve the services from.
+ /// An object that specifies the type of service object to get.
+ /// An enumeration of services of type .
+ public static IEnumerable