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

Confusing exception in case batch-registration skipped decorator #13

Closed
dotnetjunkie opened this issue Apr 10, 2015 · 0 comments
Closed
Assignees
Milestone

Comments

@dotnetjunkie
Copy link
Collaborator

The following code:

public interface IService<T> { }
public class ServiceA : IService<A> {
    public ServiceA(IValidator<A> validator) { }
}
public class ServiceB : IService<B> {
    public ServiceB(IValidator<B> validator) { }
}
public class ServiceC : IService<C> {
    public ServiceC(IService<C> validator) { } // note the cyclic dependeny here.
}
public class CController {
    public CController(IService<C> service) { }
}

container.Register(typeof(IService<>), typeof(IService<>).Assembly);

container.GetInstance<CController>();

Results in the following exception:

The registered delegate for type CController threw an exception. The constructor of type CController contains the parameter of type IService<C> with name 'service' that is not registered. Please ensure IService<C> is registered, or change the constructor of CController.

This exception is expected because Register searches for all non-generic IService<T> implementations, but skips any decorators that it encounters. ServiceC is a decorator, because it implements IService<C> and (probably) accidentally depends on IService<C> as well. This means that ServiceC is not registered and there is therefore no registration for IService<C> and this will therefore cause the "Please ensure IService is registered" message.

Although this behavior is correct and by design, it can be really confusing for the user that didn't notice his mistype in the constructor. A better exception in this case therefore might be:

The constructor of type CController contains the parameter of type IService<C> with name 'service' that is not registered. Please ensure IService<C> is registered, or change the constructor of CController. Note that ServiceC was found as implementation of IService<C>, but it was skipped by the container because it contains a cyclic reference to itself.

@dotnetjunkie dotnetjunkie modified the milestones: Simple Injector v3.0.6, Simple Injector v3.1 Sep 7, 2015
@dotnetjunkie dotnetjunkie self-assigned this Sep 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant