Checking if a service is already registered

Apr 28, 2013 at 3:14 PM
I had a method like to check whether the type was already registered:
    public bool HasComponent(Type componentType)
        return container.GetRegistration(componentType, false) != null;
After calling it though, I got an error:
"The container can't be changed after the first call to GetInstance, GetAllInstances and Verify."

I switched to this and all seems ok (tests passing):
public bool HasComponent(Type componentType)
    return container.GetCurrentRegistrations()
                                         .Any(x => x.ServiceType == componentType);
Does that implementation seem ok?
Apr 28, 2013 at 4:20 PM
So NServiceBus mixes calls to Configure with HasComponent? Nice :-S

Your solution is fine. GetCurrentRegistrations does not lock the container, while GetRegistration does.
Apr 28, 2013 at 5:06 PM
Actually it was my fault following the code from another implementation. I was trying to avoid the double registration error.
As I worked my way through their test suite, it turns out I needed to use the options:
this.container.Options.AllowOverridingRegistrations = true; 
Apr 28, 2013 at 6:58 PM
Please note that AllowOverridingRegistrations is a dangerous construct and should be used with care. It is easy to hide configuration bugs with this.
Marked as answer by dot_NET_Junkie on 11/5/2013 at 7:40 AM