caSTLe-windsor - 使用 TypedFactoryFacility 的 CaSTLe.MicroKernel.ComponentNotFoundException

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

我在使用 TypedFactoryFacility 时解决 ITcpServer 时遇到一些问题。看来Windsor没有找到合适的组件来从工厂返回该接口(interface)。我的案例的具体情况是,接口(interface) ITcpServer 是非通用的,而实现它的类都是通用的。

以下代码运行时会抛出异常:

Unhandled Exception: Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service ConsoleApplication1.StandardTcpServer`1 was found at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy) at Castle.Facilities.TypedFactory.TypedFactoryComponentResolver.Resolve(IKernelInternal kernel, IReleasePolicy scope) at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation) at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation) at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ITcpServerFactoryProxy.Create[T](String serverType, T something)

代码:

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();
}

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

最佳答案

更改以下内容:

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<>)),

祝你好运

马尔温。

关于caSTLe-windsor - 使用 TypedFactoryFacility 的 CaSTLe.MicroKernel.ComponentNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14156323/

相关文章:

c# - 无法解析非可选依赖项

c# - 从多个程序集中注册 IoC 容器

c# - Autofac 和通用类型工厂,如 CaSTLe Windsor

caSTLe-windsor - 移除温莎城堡 3 中的组件

c# - 温莎城堡 : Changes for factories and interceptors in 3. 1?

c# - 开放泛型类型的 CaSTLe Windsor Complex 注册

c# - 如何配置温莎以通过依赖树将依赖作为参数传递?

inversion-of-control - 如何以编程方式注册依赖于 CaSTLe Windsor 已注册组件列表的组件?