Search code examples
castle-windsortyped-factory-facilitywindsor-facilities

Castle.MicroKernel.ComponentNotFoundException with TypedFactoryFacility


I am having some problem resolving ITcpServer when using TypedFactoryFacility. It seems that Windsor does not find a suitable component to be returned from factory for the interface. What is specific to my case is that the interface ITcpServer is non-generic while classes implementing it are both generic.

The following code throws when run:

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)

The code:

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

Any help in solving the problem would be appreciated.


Solution

  • Change the following:

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

    and the registration:

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

    or if you only need to resolve through the factory:

    Component.For(typeof(LibUvTcpServer<>)),
    Component.For(typeof(StandardTcpServer<>)),
    

    Good luck,

    Marwijn.