Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle Windsor: suppress exceptions thrown by Resolve()

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

like image 736
Remco Ros Avatar asked Feb 24 '09 16:02

Remco Ros


1 Answers

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;
like image 96
jweber Avatar answered Oct 26 '22 07:10

jweber