Windsor Fluent Registration - All types in an assembly
-
21-09-2019 - |
문제
I wanted to use the fluent interface in Castle Windsor. Since this isn't available in the "release" binaries for .NET 2.0 I compiled from trunk, which is reported to be fairly stable. I compiled just:
- Castle.Core.dll
- Castle.DynamicProxy.dll
- Castle.MicroKernel.dll
- Castle.Windsor.dll
This should be all that is necessary for what I need it for but things aren't working as expected. I have an assembly collection which I iterate through and attempt to load all types contained within each assembly using the following code:
var container= new WindsorContainer();
foreach (var assembly in _assemblies)
{
container.Register(AllTypes.FromAssembly(assembly));
}
I stepped through the code with a debugger. _assemblies
has 2 assemblies in it. Each assembly has numerous types defined in it. The loop iterates twice without error but when it completes container
is still empty.
Update: A little clarification. The latest binaries do have the fluent interface, however they target the .NET 3.5 framework. I am working with .NET 2.0. The latest binary release to support .NET 2.0 was RC3.
해결책
The Register(AllTypes
... syntax is only the start - you have to tell Windsor what it is that you want to register.
For example to get all Controllers:
container.Register(AllTypes
.FromAssemblyContaining(representativeControllerType)
.BasedOn<Controller>()
.Configure(reg => reg
.LifeStyle.PerWebRequest));
Here's another example that registers by following the convention that all classes whose name ends in 'Service' should be registered:
container.Register(AllTypes
.FromAssemblyContaining<ConfigurationService>()
.Where(t => t.Name.EndsWith("Service", StringComparison.Ordinal))
.WithService.FirstInterface()
.Configure(reg => reg.LifeStyle.PerWebRequest));
If you truly want to register all types, you could write a Where clause that always returns true.
By default, Windsor doesn't auto-resolve concrete types.
For more information about fluent registration API read the documentation.