将参数传递给Castle Windsor Typed Factory

Tom*_*Tom 3 .net c# dependency-injection castle-windsor

我遵循此文档以使用类型化工厂并将参数传递给构造函数.当我尝试传递2个参数(1,"fo")时,类型工厂给我这个错误,如代码所示.

在此输入图像描述

public class SomeClass {
    public ITypedFactory2 F2 { get; set; } 
    public void SomeFunction() {
        var req = F2.Create<IGetFooRequest>(1, "fo"); // ERROR HERE
    }
}
public class GetFooRequest : IGetFooRequest {
    public int Bar { get; private set; }
    public string Ton { get; private set; }
    public GetFooRequest(int bar, string ton ) {
        Bar = bar;
        Ton = ton;
    }
}
public interface IGetFooRequest{
    int Bar { get; }
    string Ton { get; }
}    
public interface ITypedFactory2 {
    T Create<T>(int param1, string param2);
    void Release(object t);
}
Run Code Online (Sandbox Code Playgroud)

这是windsor安装程序的一部分......

container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ITypedFactory2>().AsFactory());
container.Register(AllTypes
            .FromAssemblyContaining<IGetFooRequest>()
            .Where(type => type.Name.EndsWith("Request"))
            .WithService.AllInterfaces().LifestyleTransient());
Run Code Online (Sandbox Code Playgroud)

为什么它说无法解决非可选依赖...?我已经过去了(1,"fo"); 我真的不明白为什么会这样......请帮忙.

小智 7

我有同样的问题,只是想出了答案.工厂方法的参数名称和类构造函数的参数名称必须匹配,不区分大小写.

所以将工厂界面更改为

public interface ITypedFactory2 {
    T Create<T>(int **bar**, string **ton**);
    void Release(object t);
}
Run Code Online (Sandbox Code Playgroud)

或者你的班级来

public class GetFooRequest : IGetFooRequest {
    public int Bar { get; private set; }
    public string Ton { get; private set; }
    public GetFooRequest(int **param1**, string **param2**) {
        Bar = bar;
        Ton = ton;
    }
}
Run Code Online (Sandbox Code Playgroud)