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

It is not possible to inject IEnumerable<CommandOption> using Microsoft.Extensions.DependencyInjection #85

Closed
mthamil opened this issue Apr 10, 2018 · 5 comments
Assignees
Labels
Milestone

Comments

@mthamil
Copy link

mthamil commented Apr 10, 2018

I am trying out the new dependency injection support for models. For additionalServices, I am passing an IServiceProvider created using ServiceCollection.BuildServiceProvider() in Microsoft.Extensions.DependencyInjection.

It seems that this service provider always returns an empty enumerable even if there are no implementations of a given service. I suppose this is by design due to the ability to retrieve all implementations of a given service. Since additional services are consulted before getting to the case where IEnumerable<CommandOption> is injected using GetOptions() in the custom CommandLineUtils service provider, these options can't be injected.

@natemcmaster
Copy link
Owner

If I understand right, you're trying to inject a constructor like this?

class MyCommand(IEnumerable<CommandOptions> options)

If so, yeah, I see the problem. It's here

if (_parent.AdditionalServices != null)
{
var retVal = _parent.AdditionalServices.GetService(serviceType);
if (retVal != null)
{
return retVal;
}
}
if (serviceType == typeof(IConsole))
{
return _parent._context.Console;
}
if (serviceType == typeof(IEnumerable<CommandOption>))
{
return _parent.GetOptions();
}
. The addition services are consulted first. Given the IEnumerable behavior is not possible to disable in Microsoft.Extensions.DependencyInjection, I don't think it should be a problem to put IEnumerable<CommandOptions> first, before resolving additional services.

@mthamil
Copy link
Author

mthamil commented Apr 10, 2018

Yes, that is exactly it. I didn't try it, but I assume it would happen with IEnumerable<CommandArgument> as well.

@natemcmaster natemcmaster added this to the 2.2.1 milestone Apr 10, 2018
@natemcmaster natemcmaster self-assigned this Apr 10, 2018
@natemcmaster
Copy link
Owner

This is an easy fix. I'm hoping to release 2.2.1 later this week, and this will be part of it.

@natemcmaster
Copy link
Owner

Fixed in 2.2.1 which is now on nuget.org

@mthamil
Copy link
Author

mthamil commented Apr 12, 2018

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants