I have a problem with resolving a Generic Interface mapped to none generic Type.
Registration:
UnityContainer.RegisterType<IHandle<SomeEvent>, SomeHandler>();
//UnityContainer.RegisterType(typeof(IHandle<SupplierApprovedEvent>), typeof(TestHandler));
This is how i try to resolve it without success:
public static void GenericResolver<T>(T args) where T : IDomainEvent
{
var handlerType = typeof(IHandle<>).MakeGenericType(args.GetType());
var firstTry = container.GetServices(handlerType);// Resolve fails
var secondTry = container.GetServices(typeof(IHandle<T>)); // Resolve fails
var casted = args as IDomainEvent;
var handlerType2 = typeof(IHandle<>).MakeGenericType(casted.GetType());
var thirdTry = container.GetServices(handlerType2);// Resolve fails
var handlerType3 = typeof(IHandle<>).MakeGenericType(typeof(T));
var fourthTry = container.GetServices(handlerType3);// Resolve fails
}
My interfaces:
public interface IDomainEvent {}
public interface IHandle<T>: IHandle where T : IDomainEvent
{
void Handle(T args);
}
public interface IHandle
{
void Handle(IDomainEvent args);
}
Interface implementation:
public class SomeHandler: IHandle<SomeEvent>
{
public void Handle(IDomainEvent args)
{
Handle(args as SomeEvent);
}
public void Handle(SomeEvent args)
{
//DO SOMETHING
}
}
What do I miss here :!?
UPDATE:
1.there are no exceptions.
2.GetService is returning null.
3.This resolve works fine but its not what i want:
var itsTypeofSomeHangled =(SomeHandler)injector.GetService(typeof(IHandle<SomeEvent>));
4.container in the example is UnityDependencyResolver which inherits IDependencyResolver. This is the reason why i call methods GetServices and GerSevice
UPDATE2: It turns out that the problem is in GetServices (ResolveAll). This line works perfectly, but i have more than one implementation of this Gereric Interface.
var handlerType = typeof(IHandle<>).MakeGenericType(args.GetType());
var xxxx = (IHandle)container.GetService(handlerType);
GetServices
an extension method that you created yourself? Or do you mean to useResolve
instead? – Yacoub Massadcontainer.GetServices
instead ofcontainer.Resolve
? – StevenGetServices
instead ofGetService
? – Yacoub Massad