Search code examples
.net-corecastle-windsortyped-factory-facility

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


I have a Typed Factory in Castle Windsor registered like

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

and the interface registered like

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

I have also registered the facility like

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

When I try to resolve an instance of IMyClass like

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

an exception is thrown with the message

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?'

My IMyTypeFactory looks like this:

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

MyType constructor looks like this:

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

Solution

  • I can see how that can be confusing. It has nothing to do with namespace.

    Your factory method is called GetMyClass and 'Get' methods lookup components by name so Windsor looks for a component named MyClass, which happens to be the name of your class sans namespace.

    To turn that off change the factory registration to .AsFactory(new DefaultTypedFactoryComponentSelector(getMethodsResolveByName: false)));

    Or rename the method.