Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for keyed services #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 63 additions & 27 deletions src/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IServiceProvider>();
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<IServiceProvider>();
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<IServiceProvider>();
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)
Expand Down
23 changes: 23 additions & 0 deletions src/ServiceProvider/ServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Unity.Microsoft.DependencyInjection
{
public class ServiceProvider : IServiceProvider,
ISupportRequiredService,
IKeyedServiceProvider,
IServiceScopeFactory,
IServiceScope,
IDisposable
Expand Down Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions src/Unity.Microsoft.DependencyInjection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<DebugType>Portable</DebugType>
</PropertyGroup>

Expand All @@ -51,7 +51,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>

<!-- Sourcelink -->
Expand Down