Why can't I register different components implemented by the same type? (Castle Windsor)

StackOverflow https://stackoverflow.com/questions/22323113

Вопрос

I have the following:

interface IMediator { }


interface IDistanceRetriever { }

class Mediator : IMediator, IDistanceRetriever{ }

When I call the following in my WindsorInstaller,

container.Register(Component.For<IMediator>).ImplementedBy<Mediator>().LifeStyle.Singleton);
container.Register(Component.For<IDistanceRetriever>).ImplementedBy<Mediator>().LifeStyle.Singleton);

I get the following exception message: Castle.MicroKernel.ComponentRegistrationException: There is a component already registered for the given key ...Mediator

Why can't I register different components implemented by the same type?

Это было полезно?

Решение

You have to register in this way

container.Register(Component.For<IMediator, IDistanceRetriever>).ImplementedBy<Mediator>().LifeStyle.Singleton);

See documention on Castle Project

Другие советы

Instead of registering components one-by-one you can register components by conventions and use WithServiceAllInterfaces() method.

container.Register(Classes
    .FromThisAssembly()
    .Where(type => type == typeof (Mediator))
    .WithServiceAllInterfaces()
    .LifestyleSingleton());

The first two answers will solve your problem. I believe the core problem is that Windsor needs to be able to individually identify the service and implementation pair by name. So if you are going to register the services for that implementation individually, then you must provide a name with each registration.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top