Search code examples
castle-windsor

Check if Windsor has a matching component registered


In a Castle Windsor scenario I want to check if my container has a certain service registered, and do basically

if (container.HasComponentFor<IMyService>()) {
    // resolve service with container.Resolve<IMyService>()
    // then do cool stuff
}

but of course, container.HasComponentFor<IMyService>() doesn't exist. Is there an equivalent?


Solution

  • You can check if the MicroKernel has the component registered:

    if (container.Kernel.HasComponent(typeof(IMyService)))
        // resolve service with container.Resolve<IMyService>()
        // then do cool stuff
    }