Skip to content

Validate presence of constructor argument types when calling BuildServiceProvider #2348

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

Closed
aspnet-hello opened this issue Jan 1, 2018 · 2 comments
Assignees
Labels
Done This issue has been fixed enhancement This issue represents an ask for new feature or an enhancement to an existing one

Comments

@aspnet-hello
Copy link

From @spartan563 on Thursday, March 24, 2016 6:35:45 AM

Background

When defining a service which accepts dependencies through its constructor, you expect that all required services are present in the container for its construction. One would expect that, failing this contract, an exception would be thrown when building the container.

Issue

At the moment, it is possible to register a service whose constructor requires services not present in the container. Building of the container (using BuildServiceProvider) succeeds and one will only encounter an error when you attempt to retrieve an instance of that service (or a service depending on it) through the GetService call.

From an architectural perspective, this allows for certain edge cases to only be hit when the services are eventually requested at runtime, not necessarily at application start. Ideally, the application should fail to start should a condition like this be possible.

Reproduction

public class A {
  public A(B b) {

  }
}

public class B {

}

var services = new ServiceCollection();
services.AddSingleton<A>();

var container = services.BuildServiceProvider(); // Expected an exception here

container.GetService<A>(); // Throws exception here

Suggestions

In an ideal world, this would be done as part of building up your call sites and within the call to BuildServiceProvider - throwing an exception if a dependency was not present in the container.

I understand this may not necessarily be possible, especially if you only build up the call sites when they are first required, in which case perhaps we can look at alternative solutions.

/cc @wesleyolis

Copied from original issue: aspnet/DependencyInjection#392

@aspnet-hello
Copy link
Author

From @Eilon on Tuesday, April 19, 2016 11:34:32 AM

Definitely an interesting idea, but not planning to build it at this time.

@davidfowl
Copy link
Member

A couple of things:

  • We can't do this by default, it needs to be a flag.
  • You cannot do this for open generics.

We'll investigate for 3.0

@davidfowl davidfowl added the enhancement This issue represents an ask for new feature or an enhancement to an existing one label Oct 17, 2018
@pakrym pakrym self-assigned this Nov 16, 2018
@pranavkm pranavkm added Done This issue has been fixed and removed 2 - Working labels Apr 18, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Done This issue has been fixed enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

No branches or pull requests

4 participants