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

Improve the error message when parameter type is not registered #175

Merged
merged 2 commits into from
Nov 24, 2018
Merged

Improve the error message when parameter type is not registered #175

merged 2 commits into from
Nov 24, 2018

Conversation

akunzai
Copy link
Contributor

@akunzai akunzai commented Oct 31, 2018

Throws InvalidOperationException with following message when parameter type is not registered in ConstructorInjectionConvention:

The constructor of type '{modelType}' contains the parameter of type '{paramType}' is not registered, Ensure the type '{paramType}' are registered in additional services with CommandLineApplication.Conventions.UseConstructorInjection(IServiceProvider additionalServices)

Fix #174

@natemcmaster
Copy link
Owner

I agree it would be good to add better error messages, but I think this is a subtle breaking change for types which have multiple constructors. Let's try to preserve the current behavior.

I'm concerned about a scenario like this:

public class MyType
{
  public MyType(IService1 s1) { } // ctor A
  public MyType(IService1 s1, IService2 s2) { } // ctor B
}

If only IService1 were registered in the service provider, this convention would try first to use ctor B, and then fallback to ctor A. With your change, it would throw. I can see benefits to having a stricter behavior as the default, but I don't want to break existing users.

If you can think of a way to keep existing behavior and improve it, I'll reconsider. For example, adding a "strict mode" options flag, or throwing a better error after all constructors have been examined.

Thanks,
Nate

@akunzai
Copy link
Contributor Author

akunzai commented Nov 5, 2018

Thanks @natemcmaster

I will study again to see how to deal with it better.

…ructor and throw better error message

- throw InvalidOperationException if single constructor found, but no parameter type registered
- throw InvalidOperationException if no matched constructor and no parameter-less constructor found
- skip apply convention if we found parameter-less constructor
@akunzai
Copy link
Contributor Author

akunzai commented Nov 24, 2018

Hi @natemcmaster,

Can we use ActivatorUtilities from Microsoft.Extensions.DependencyInjection.Abstractions to simplify this

@natemcmaster
Copy link
Owner

Thanks for updating the PR. This seems fine to me.

Can we use ActivatorUtilities from Microsoft.Extensions.DependencyInjection.Abstractions to simplify this

No. That would add a new dependency which I don't want this library to have.

@natemcmaster natemcmaster merged commit d72af41 into natemcmaster:master Nov 24, 2018
@akunzai akunzai deleted the feature/improve-parameter-type-not-registered branch November 25, 2018 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants