R – Castle Windsor: suppress exceptions thrown by Resolve()

castle-windsornet

When resolving a component which the Windsor container cannot find, an exception is thrown.

StructureMap has a TryGetInstance method, which returns null of it can't find the requested component.

Does Castle Windsor has something like this? Or am I forced to catch these exceptions (I don't like that, because of the performance overhead of throwing and catching exceptions).

Thanks in advance,

Remco

Best Answer

You can check if the MicroKernel has an instance of the component registered before calling the Resolve method of the Windsor container.

Something like the following should work.

if ( windsor.Kernel.HasComponent( componentType ) )
{
    return windsor.Resolve( componentType );
}

return null;
Related Topic