Resolving a Generic with a Generic parameter in Castle Windsor
- by Aaron Fischer
I am trying to register a type like IRequestHandler1[GenericTestRequest1[T]] which will be implemented by GenericTestRequestHandler`1[T] but I am currently getting an error from Windsor "Castle.MicroKernel.ComponentNotFoundException : No component for supporting the service " Is this type of operation supported? Or is it to far removed from the suppored register( Component.For(typeof( IList<).ImplementedBy( typeof( List< ) ) )
below is an example of a breaking test.
//////////////////////////////////////////////////////
public interface IRequestHandler{}
public interface IRequestHandler<TRequest> : IRequestHandler where TRequest : Request{}
public class GenericTestRequest<T> : Request{}
public class GenericTestRequestHandler<T> : RequestHandler<GenericTestRequest<T>>{}
[TestFixture]
public class ComponentRegistrationTests{
[Test]
public void DoNotAutoRegisterGenericRequestHandler(){
var IOC = new Castle.Windsor.WindsorContainer();
var type = typeof( IRequestHandler<> ).MakeGenericType( typeof( GenericTestRequest<> ) );
IOC.Register( Component.For( type ).ImplementedBy( typeof( GenericTestRequestHandler<> ) ) );
var requestHandler = IoC.Container.Resolve( typeof(IRequestHandler<GenericTestRequest<String>>));
Assert.IsInstanceOf <IRequestHandler<GenericTestRequest<String>>>( requestHandler );
Assert.IsNotNull( requestHandler );
}
}