Hi All.

Can anybody explain why Windsor will not resolve the component.


using System;
using System.Collections.Generic;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
namespace WindsorIOCProblem
{
    class Program
    {
        static void Main(string[] args)
        {
            IWindsorContainer container = new WindsorContainer();
 
container.Register(Component.For<IEventHandler<SomethingCreatedEvent>>().ImplementedBy<CreateSomethingEventHandler>());

            var publisher = new Publisher(container);

            // This works
            var created = new SomethingCreatedEvent();
            publisher.Publish(created);

            // This does not.
            var list = new List<IEvent>() { new
SomethingCreatedEvent() };
            foreach (var e in list)
                publisher.Publish(e);

        }
    }

    public interface IEvent { }

    public class SomethingCreatedEvent : IEvent { }

    public interface IEventHandler<T> where T : IEvent
    {
        void Handle(IEvent @event);
    }

    public class CreateSomethingEventHandler :
IEventHandler<SomethingCreatedEvent>
    {
        void IEventHandler<SomethingCreatedEvent>.Handle(IEvent
@event)
        {
            Console.WriteLine(this.GetType().Name);
        }
    }

    public class Publisher
    {
        public Publisher(IWindsorContainer container)
        {
            _container = container;
        }

        public void Publish<T>(T @event) where T : IEvent
        {
            var handler = _container.Resolve<IEventHandler<T>>();
            handler.Handle(@event);
        }

        private IWindsorContainer _container;
    }
}

In the code example I give (which should run with a reference to
Windsor).
If I call Publisher.Publish with the explicit type Windsor can resolve
the component. However if I use the second method (which is what to
do). Windsor will not resolve the component and throws an exception
telling me it cannot find a service supporting IEvent. I can
understand why Windsor cannot find a service implementing IEvent.
My question is how can I Windsor to resolve correctly?

Thanks

Paul

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to