diff --git a/src/Configuration.cs b/src/Configuration.cs index 9837788..4c2dcbb 100644 --- a/src/Configuration.cs +++ b/src/Configuration.cs @@ -27,40 +27,76 @@ internal static IUnityContainer AddServices(this IUnityContainer container, ISer internal static void Register(this IUnityContainer container, ServiceDescriptor serviceDescriptor, ILifetimeContainer lifetime) { - if (serviceDescriptor.ImplementationType != null) + if (serviceDescriptor.IsKeyedService) { - var name = serviceDescriptor.ServiceType.IsGenericTypeDefinition ? UnityContainer.All : null; - container.RegisterType(serviceDescriptor.ServiceType, - serviceDescriptor.ImplementationType, - name, - (ITypeLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); - } - else if (serviceDescriptor.ImplementationFactory != null) - { - container.RegisterFactory(serviceDescriptor.ServiceType, - null, - scope => - { - var serviceProvider = scope.Resolve(); - var instance = serviceDescriptor.ImplementationFactory(serviceProvider); - return instance; - }, - (IFactoryLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); - } - else if (serviceDescriptor.ImplementationInstance != null) - { - container.RegisterInstance(serviceDescriptor.ServiceType, - null, - serviceDescriptor.ImplementationInstance, - (IInstanceLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); + if (serviceDescriptor.KeyedImplementationType != null) + { + var name = serviceDescriptor.ServiceType.IsGenericTypeDefinition ? UnityContainer.All : null; + container.RegisterType(serviceDescriptor.ServiceType, + serviceDescriptor.KeyedImplementationType, + name, + (ITypeLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); + } + else if (serviceDescriptor.KeyedImplementationFactory != null) + { + container.RegisterFactory(serviceDescriptor.ServiceType, + (string)serviceDescriptor.ServiceKey, + scope => + { + var serviceProvider = scope.Resolve(); + var instance = serviceDescriptor.KeyedImplementationFactory(serviceProvider, serviceDescriptor.ServiceKey); + return instance; + }, + (IFactoryLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); + } + else if (serviceDescriptor.KeyedImplementationInstance != null) + { + container.RegisterInstance(serviceDescriptor.ServiceType, + (string)serviceDescriptor.ServiceKey, + serviceDescriptor.KeyedImplementationInstance, + (IInstanceLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); + } + else + { + throw new InvalidOperationException("Unsupported registration type"); + } } else { - throw new InvalidOperationException("Unsupported registration type"); + if (serviceDescriptor.ImplementationType != null) + { + var name = serviceDescriptor.ServiceType.IsGenericTypeDefinition ? UnityContainer.All : null; + container.RegisterType(serviceDescriptor.ServiceType, + serviceDescriptor.ImplementationType, + name, + (ITypeLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); + } + else if (serviceDescriptor.ImplementationFactory != null) + { + container.RegisterFactory(serviceDescriptor.ServiceType, + null, + scope => + { + var serviceProvider = scope.Resolve(); + var instance = serviceDescriptor.ImplementationFactory(serviceProvider); + return instance; + }, + (IFactoryLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); + } + else if (serviceDescriptor.ImplementationInstance != null) + { + container.RegisterInstance(serviceDescriptor.ServiceType, + null, + serviceDescriptor.ImplementationInstance, + (IInstanceLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); + } + else + { + throw new InvalidOperationException("Unsupported registration type"); + } } } - internal static LifetimeManager GetLifetime(this ServiceDescriptor serviceDescriptor, ILifetimeContainer lifetime) { switch (serviceDescriptor.Lifetime) diff --git a/src/ServiceProvider/ServiceProvider.cs b/src/ServiceProvider/ServiceProvider.cs index ccb8e3b..521182f 100644 --- a/src/ServiceProvider/ServiceProvider.cs +++ b/src/ServiceProvider/ServiceProvider.cs @@ -7,6 +7,7 @@ namespace Unity.Microsoft.DependencyInjection { public class ServiceProvider : IServiceProvider, ISupportRequiredService, + IKeyedServiceProvider, IServiceScopeFactory, IServiceScope, IDisposable @@ -102,6 +103,28 @@ public void Dispose() GC.SuppressFinalize(this); } + public object GetKeyedService(Type serviceType, object serviceKey) + { + if (null == _container) + throw new ObjectDisposedException(nameof(IServiceProvider)); + + try + { + return _container.Resolve(serviceType, (string) serviceKey); + } + catch { /* Ignore */} + + return null; + } + + public object GetRequiredKeyedService(Type serviceType, object serviceKey) + { + if (null == _container) + throw new ObjectDisposedException(nameof(IServiceProvider)); + + return _container.Resolve(serviceType, (string) serviceKey); + } + #endregion } } diff --git a/src/Unity.Microsoft.DependencyInjection.csproj b/src/Unity.Microsoft.DependencyInjection.csproj index 5830d40..e006a79 100644 --- a/src/Unity.Microsoft.DependencyInjection.csproj +++ b/src/Unity.Microsoft.DependencyInjection.csproj @@ -31,7 +31,7 @@ true - netcoreapp3.1;netstandard2.0 + netstandard2.0 Portable @@ -51,7 +51,7 @@ - +