Castle Windsor: How to register internal implementations

Deadly 提交于 2019-12-10 19:33:40

问题


This registration works when all the implementations of IService are public:

AllTypes
    .Of<IService>()
    .FromAssembly(GetType().Assembly)
    .WithService.FirstInterface()

For example:

public interface IService {}
public interface ISomeService : IService {}
public class SomeService : ISomeService {}

Resolving ISomeService returns an instance of SomeService.

But if the SomeService class is internal, the container throws an exception that states that ISomeService is not registered.

So is there any way to register internal implementations for public interfaces?


回答1:


UPDATE2: While I consider this not a good idea, you can achieve that with the following code.

AllTypes.FromAssembly(GetType().Assembly)
    .IncludeNonPublicTypes()
    .BasedOn<IService>()
    .WithService.FirstInterface();

UPDATE: This is a very logical behavior and I would consider this a bug if it was otherwise. AllTypes exposes only public type, because it assumes, if you made your type internal, you did it for a reason, and if it exposed it it could be a security issue.

When you register type explicitly on the other hand, it works because Windsor assumes, since you're explicitly asking it to use an internal type, you know better.




来源:https://stackoverflow.com/questions/1837020/castle-windsor-how-to-register-internal-implementations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!