Castle Windsor: suppress exceptions thrown by Resolve()
Asked Answered
C

1

12

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

Calci answered 24/2, 2009 at 16:25 Comment(0)
E
15

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;
Enroll answered 24/2, 2009 at 16:36 Comment(1)
Yes, but it doesn't have an overload to Resolve by name AND type. only one for name and one for Type.Calci

© 2022 - 2024 — McMap. All rights reserved.