带有TypedFactoryFacility的Castle.MicroKernel.ComponentNotFoundException

时间:2013-01-04 11:30:48

标签: castle-windsor typed-factory-facility windsor-facilities

使用TypedFactoryFacility时,我在解决ITcpServer时遇到了一些问题。似乎Windsor没有找到适合从工厂返回的接口组件。具体到我的情况是,接口ITcpServer是非泛型的,而实现它的类是通用的。

运行时抛出以下代码:

  

未处理的异常:Castle.MicroKernel.ComponentNotFoundException:否   支持服务的组件   找到ConsoleApplication1.StandardTcpServer`1   Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(类型   服务,IDictionary参数,IReleasePolicy政策)at   Castle.Facilities.TypedFactory.TypedFactoryComponentResolver.Resolve(IKernelInternal   内核,IReleasePolicy范围)   Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation   调用)   Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation   (在Castle.DynamicProxy.AbstractInvocation.Proceed()上调用)   在Castle.Proxies.ITcpServerFactoryProxy.Create [T](String serverType,   什么)

代码:

class Program
{
    static void Main(string[] args)
    {
        IWindsorContainer container = new WindsorContainer();
        container.Install(new MyWindsorInstaller());

        var f = container.Resolve<ITcpServerFactory>();
        var tcpServer = f.Create("standard", "something");
        tcpServer.Start();
    }
}

 public class MyWindsorInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<TypedFactoryFacility>();
        container.Register(
            Component.For<ITcpServer>().ImplementedBy(typeof(LibUvTcpServer<>)),
            Component.For<ITcpServer>().ImplementedBy(typeof(StandardTcpServer<>)),
            Component.For<ITcpServerFactory>().AsFactory(c => c.SelectedWith(new TcpServerComponentSelector()))
            );
    }
}

public class TcpServerComponentSelector : DefaultTypedFactoryComponentSelector
{
    protected override Type GetComponentType(System.Reflection.MethodInfo method, object[] arguments)
    {
        var serverType = (string)arguments.First();
        return serverType == "standard" ? typeof (StandardTcpServer<>) : typeof(LibUvTcpServer<>);
    }
}

public interface ITcpServerFactory
{
    ITcpServer Create<T>(string serverType, T something);
}

public class StandardTcpServer<T> : ITcpServer
{
    public void Start()
    {
        Console.WriteLine("Started...");
    }
}

public class LibUvTcpServer<T> : ITcpServer
{
    private readonly T something;

    public LibUvTcpServer(T something)
    {
        this.something = something;
    }

    public void Start()
    {
        Console.WriteLine("Started...");
    }
}

public interface ITcpServer
{
    void Start();
}

任何帮助解决问题的人都将不胜感激。

1 个答案:

答案 0 :(得分:1)

更改以下内容:

protected override Type GetComponentType(System.Reflection.MethodInfo method, object[] arguments)
{
    var serverType = (string)arguments.First();
    if (serverType == "standard") 
    {
        return typeof(StandardTcpServer<>).MakeGenericType(arguments[1].GetType());
    }
    else
    {
        return typeof(LibUvTcpServer<>).MakeGenericType(arguments[1].GetType());
    }
}

和注册:

Component.For<ITcpServer>().Forward(typeof(LibUvTcpServer<>)).ImplementedBy(typeof(LibUvTcpServer<>)),
Component.For<ITcpServer>().Forward(typeof(StandardTcpServer<>)).ImplementedBy(typeof(StandardTcpServer<>)),

或者如果您只需要通过工厂解决:

Component.For(typeof(LibUvTcpServer<>)),
Component.For(typeof(StandardTcpServer<>)),
祝你好运,

Marwijn。