Castle Windsor Typed Factory Facility 似乎找不到接口,因为它的命名空间

Castle Windsor Typed Factory Facility cannot find interface seemingly because of its namespace

我在 Castle Windsor 有一家类型化工厂注册为

container.Register(Component.For<IMyTypeFactory>().AsFactory());

并且接口注册为

container.Register(Component.For<IMyType>()
                .ImplementedBy<MyType>().LifestyleTransient()
                .Interceptors(typeof(MyInterceptor));

我也注册了类似的设施

if (!container.Kernel.GetFacilities().Any(f => f is TypedFactoryFacility))
{
  container.AddFacility<TypedFactoryFacility>(); // Due to the debugger this line is reached.
}

当我尝试解析 IMyClass 的实例时,例如

var myFactory = container.Resolve<IMyFactory>();
var myClass = myFactory.GetMyClass(myParameter); // This is where I get the exception

抛出异常并显示消息

Castle.MicroKernel.ComponentNotFoundException: 'Requested component named 'IMyClass' was not found in the container. Did you forget to register it?
There is one other component supporting requested service 'The.Name.Space.IMyClass'. Is it what you were looking for?'

我的 IMyTypeFactory 如下所示:

public interface IMyTypeFactory 
{
  IMyType GetIMyType(IMyParameter myParameter);
}

MyType 构造函数如下所示:

public MyType(
  IOneProperlyResolvedType oneProperlyResolvedType, 
  IAnotherProperlyResolvedType anotherProperlyResolvedType, 
  IMyParameter myParameter)
        {
            this.oneProperlyResolvedType = oneProperlyResolvedType;
            this.anotherProperlyResolvedType = anotherProperlyResolvedType;
            this.myParameter = myParameter;
        }

我知道这会让人感到困惑。与命名空间无关。

你的工厂方法被称为 GetMyClass'Get' methods lookup components by name 所以 Windsor 寻找一个组件 named MyClass,这恰好是名称你的 class 没有命名空间。

要关闭它,请将工厂注册更改为 .AsFactory(new DefaultTypedFactoryComponentSelector(getMethodsResolveByName: false)));

或重命名该方法。