Castle Windsor: is there a way of validating registration without a resolve call?

后端 未结 1 1405
小鲜肉
小鲜肉 2021-02-04 04:10

My current understanding of Castle Windsor registration is that one can only validate registration by calling Resolve on a root component. But since windsor\'s component model k

相关标签:
1条回答
  • OK, It is possible. Thanks to Krzysztof Koźmic for showing me how. Not immediately obvious, but you can use Windsor's diagnostic subsystem to raise potential problems with registration. I've put together a little static method that throws if there are any misconfigured components:

    private static void CheckForPotentiallyMisconfiguredComponents(IWindsorContainer container)
    {
        var host = (IDiagnosticsHost)container.Kernel.GetSubSystem(SubSystemConstants.DiagnosticsKey);
        var diagnostics = host.GetDiagnostic<IPotentiallyMisconfiguredComponentsDiagnostic>();
    
        var handlers = diagnostics.Inspect();
    
        if (handlers.Any())
        {
            var message = new StringBuilder();
            var inspector = new DependencyInspector(message);
    
            foreach (IExposeDependencyInfo handler in handlers)
            {
                handler.ObtainDependencyDetails(inspector);
            }
    
            throw new MisconfiguredComponentException(message.ToString());
        }
    }
    

    You can use it like this:

    var container = new WindsorContainer().Register(
        Component.For<IRoot>().ImplementedBy<Root>()
        );
    
    CheckForPotentiallyMisconfiguredComponents(container);
    

    In this case I get a MisconfiguredComponentException with this message:

    'WindsorSpikes.Root' is waiting for the following dependencies:
    - Service 'WindsorSpikes.IChild' which was not registered.
    
    WindsorSpikes.MisconfiguredComponentException:
    'WindsorSpikes.Root' is waiting for the following dependencies:
    - Service 'WindsorSpikes.IChild' which was not registered.
    

    See the castle documentation for more details on the diagnostic subsystem:

    http://stw.castleproject.org/Default.aspx?Page=Debugger-views&NS=Windsor

    0 讨论(0)
提交回复
热议问题