-
-
Notifications
You must be signed in to change notification settings - Fork 122
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
RegisterAssembly duplicates services registrations #554
Comments
Actually this is not a bug and it has been this way all the time. When scanning an assembly without an In this case we will get two services like this
Notice that the implementing type So next we ask for all services with service type var actualServices = container.GetInstance<IEnumerable<IDoSomeWork>>(); Since variance is enabled by default we will get all services that implements The recommended way of dealing with this is to implement an public class CompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register<IDoSomeWork, DoSomeWork>();
}
} This is much more descriptive and also prevents the container to be filled up with registrations that are not going to be used. |
Thanks for the explanation! Yes, I`ve found out this two registration in the sources and understand now how it works. But still, if not pay attention to the container Concerning |
The reason it works this way is that not all services are backed by an interface. Even a simple class like this can be a service and LightInject takes the route of registering all classes as services. public class Foo
{
} But this behaviour can be changed container.RegisterAssembly(assembly, (serviceType, implementingType) => serviceType.IsInterface); or any other condition we should want for registering services 👍😊 |
Yep, this works, thanks! |
Hi!
When I use RegisterAssembly method to add types from some library to the container and then try to resolve IEnumerable of some service, I get 2x instances.
For instance, if I have only one implementation of an interface and ask the container for all possible implementations, I expect to get the only implementation, but it provides me with two services, which looks like a bug:
The text was updated successfully, but these errors were encountered: