简体   繁体   中英

Castle windsor registering open generics

I'm trying to register some open generics

If I do it like this it works

Component
    .For(typeof(IDtoFactory<>))
    .ImplementedBy(typeof(DtoFactoryBase<>))
    .LifestyleSingleton()

However if I try to register like this it fails

Classes
    .FromThisAssembly()
    .BasedOn(typeof(IDtoFactory<>))
    .WithServiceAllInterfaces()
    .LifestyleSingleton()

I don't really mind having to have an abstract base class to make this work but it's a bit of a pain nonetheless as I'd be happy with all the factories just implementing the interface.

I get an error like the following

Service 'MyProj.Factories.IDtoFactory`1
[[MyProject.Models.MemberDto, 
  MyProject, 
  Version=2.0.0.1, 
  Culture=neutral, 
  PublicKeyToken=null]]' which was not registered.

I'm using the latest version of Castle Windsor ~3.3

Am I doing anything wrong with my Classes registration for an open generic?


So this is incredibly frustrating

I went to build an example of it not working here and it worked first time with no problems. I'll have to look into my implementation at work because clearly there's some differences.

Is there any chance that the error is not with the registration of IDtoFactory itself but with the connection between it and your Dto`s? Can you paste some more code? In order to see what is actually going on expose your container from a property (for the test sake) and take a look at the component registrations. You will clearly see if there are any misconfigured components - there is such a property in the container itself.

I found out my issue here. In my production environment I had the following in a different installer

 Classes
    .FromThisAssembly()
    .Where(type => 
        type.Name.EndsWith("Wrapper") || 
        type.Name.EndsWith("Provider") ||
        type.Name.EndsWith("Factory") ||
        type.Name.EndsWith("Reader") ||
        type.Name.EndsWith("Writer") ||
        type.Name.EndsWith("Destroyer") ||
        type.Name.EndsWith("Helper")
    )
    .WithService
    .DefaultInterfaces()
    .Configure(c => c.LifeStyle.Is(scope))

This was messing up my other installer. Once I removed it and replaced the registrations with something more explicit everything started working correctly

here is a working example on github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM