Skip to content

Commit

Permalink
All tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
seesharper committed Sep 18, 2024
1 parent fff3a05 commit 9d5028b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/LightInject.Tests/KeyedMicrosoftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ public void ResolveKeyedServiceSingletonInstanceWithAnyKey()
Assert.Equal(serviceKey2, svc2.ToString());
}

[Fact]
public void ResolveKeyedServicesSingletonInstanceWithAnyKey()
{
var container = CreateContainer();
var rootScope = container.BeginScope();
var service1 = new FakeService();
var service2 = new FakeService();

var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedSingleton<IFakeOpenGenericService<PocoClass>>(KeyedService.AnyKey, service1);
serviceCollection.AddKeyedSingleton<IFakeOpenGenericService<PocoClass>>("some-key", service2);
container.Register<IFakeOpenGenericService<PocoClass>>(sf => service1, KeyedService.AnyKey.ToString(), new PerRootScopeLifetime(rootScope));
container.Register<IFakeOpenGenericService<PocoClass>>(sf => service2, "some-key", new PerRootScopeLifetime(rootScope));

// container.RegisterInstance<IFakeOpenGenericService<PocoClass>>(service1, KeyedService.AnyKey.ToString());
// container.RegisterInstance<IFakeOpenGenericService<PocoClass>>(service2, "some-key");


// var provider = CreateServiceProvider(serviceCollection);
var services = rootScope.GetInstance<IEnumerable<IFakeOpenGenericService<PocoClass>>>("some-key").ToList();
// var services = provider.GetKeyedServices<IFakeOpenGenericService<PocoClass>>("some-key").ToList();
Assert.Equal(new[] { service1, service2 }, services);
}


internal interface IService { }
Expand Down
14 changes: 5 additions & 9 deletions src/LightInject/LightInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4838,18 +4838,14 @@ private Action<IEmitter> CreateEmitMethodForEnumerableServiceServiceRequest(Type
{
if (serviceName == "*")
{
// var serviceKeys = allEmitters.Keys.Where(k => actualServiceType.IsAssignableFrom(k.ServiceType) && k.ServiceName.Length > 0).ToList();

// var query = from r in allRegistrations.SelectMany(r => r.Value)
// join k in serviceKeys on new ServiceKey(r.ServiceType, r.ServiceName) equals k
// select r;


// allRegistrations.Join(serviceKeys, r => new ServiceKey(r.ServiceType, r.ServiceName), k => k, (r, k) => r).ToList().ForEach(r => Register(r));

emitMethods = allEmitters.Keys.Where(k => actualServiceType.IsAssignableFrom(k.ServiceType) && k.ServiceName.Length > 0).SelectMany(k => allEmitters[k]).Where(emi => !emi.CreatedFromWildcardService).OrderBy(emi => emi.RegistrationOrder).Select(emi => emi.EmitMethod).ToList();
}
else
if (serviceName.Length > 0)
{
emitMethods = allEmitters.Keys.Where(k => actualServiceType.IsAssignableFrom(k.ServiceType) && k.ServiceName == serviceName || k.ServiceName == "*").SelectMany(k => allEmitters[k]).Where(emi => !emi.CreatedFromWildcardService).OrderBy(emi => emi.RegistrationOrder).Select(emi => emi.EmitMethod).ToList();
}
else
{
var serviceKeys = allEmitters.Keys.Where(k => actualServiceType.IsAssignableFrom(k.ServiceType) && k.ServiceName == serviceName).ToList();
emitMethods = serviceKeys.SelectMany(k => allEmitters[k]).Select(emi => emi.EmitMethod).ToList();
Expand Down

0 comments on commit 9d5028b

Please sign in to comment.