-
Notifications
You must be signed in to change notification settings - Fork 320
"Cannot instantiate implementation type" even when specifying generic type #531
Comments
Can you provide a minimal repro please? |
@pakrym Of course, here it is: container_sample.zip |
Exception is by design,
and this is not supported by DI container. Only first order open generic registrations are supported. |
@pakrym I have to admit I don't quite understand why this scenario isn't supported though. It seems to me a common use case. Is there any recommendations on how to achieve similar behaviour (extending the default container or not) besides replacing it? |
@thiagomajesk it creates a lot of complexities in resolution rules, imagine following registrations:
Which one would you use to resolve The only way to make it work with AspNetCore.DependencyInjection is too define interface |
@pakrym I see your point now, in that example the container doesn't know how to act because it can't tell the difference between So if I got it right and I still want this behaviour, I'd have to simplify the generic registration through the interface that has just one level of generics, like you've just demonstrated. So, for sake of visualization, something like this:
|
@thiagomajesk yes, we don't try to deconstruct generic type 'trees'. In your example you can have |
@pakrym Have you tested this workaround? It seems that even when using the interface approach it doesn't work. It throws the same error on
public class Command<T> : ICommand<T>
{
public List<T> List { get; set; }
} public class CommandHandler<T> : ICommandHandler<T>
{
public Command<T> Handle(Command<T> message) { [...] }
} Any thoughts? |
This ambiguity situation could actually still be unsupported (and e.g. throw an error on startup, as today), while still supporting having just one of them registered. I stumbled here also from MediatR, and the case I want to get working is having the following interfaces and concrete implementations:
Given an instance of Of course, it's possible to come up with situations that are close to this but ambiguous - but even with all its flexibility, the example above is not. (And I'm thinking that the generic type constraint on |
This issue was moved to dotnet/aspnetcore#2341 |
Hello, I'm having trouble using the default container with @jbogard's MediatR.
I know that currently there are limitations, but I would appreciated if someone could bring any help.
I have the standard generic command:
and its handler:
I was searching about it and saw that the user @dannyc02 managed to get this working with Structure Map by specifying the concrete types during registration (jbogard/MediatR#69). I've tried the same thing without success:
services.AddTransient(typeof(IRequestHandler<Command<string>, Command<string>>), typeof(CommandHandler<string>));
If its possible to register the concrete type like the example I know I could write a custom type scan to register everything to me, like this: https://gist.github.com/thiagomajesk/204ca7e602c3a1f1d5cc6f7b4ab42aa9. But right now I only get
Cannot instantiate implementation type
.The text was updated successfully, but these errors were encountered: