JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
568 stars 119 forks source link

Using Lamar with Microsoft Hosting/DependencyInjection is returning too many instances. #152

Closed hellfirehd closed 5 years ago

hellfirehd commented 5 years ago

Basically, I'm trying to use Lamar for it's type scanning and registries while still relying on Microsoft's DI framework. In particular, I want ConnectImplementationsToTypesClosing(typeof(IHandleMessages<>)).

However, once the host is built and the ServiceProvider is returned, ServiceProvider.GetServices<IHandleMessages<FooMessage>>() gives me back two instances when there should only be one, and four when there should be two.

This gist demonstrates it perfectly.

Can anyone tell me what I'm doing wrong? (I mean, besides using Microsoft.Extensions.DependencyInjection instead of just pure Lamar. 😛)

CodingGorilla commented 5 years ago

This one is funny, I just spent two hours debugging this and realized the solutions is really simple. Comment out your .ConfigureContainer() in the failing test and run it again. 😄

You're creating a new container against an existing ServiceRegistry, the new container runs the scan a second time, adding all the registrations to the existing ServiceRegistry again.

hellfirehd commented 5 years ago

D'oh! Thank you so much!