ServiceLocator.Current throws NullReferenceException


When no IoC container is registered using ServiceLocator.SetLocatorProvider(ServiceLocatorProvider) the Current property will throw a NullReferenceException. Throwing a NullReferenceException is always a bad thing for a reusable framework such as CSL, or to quote the Framework Design Guidelines: "DO NOT allow publicly callable APIs to explicitly or implicitly throw NullReferenceException".
The Current property should check if the currentProvider delegate is null and if so, throw an InvalidOperationException with an expressive exception message, explaining what is wrong and what should be done to resolve this issue.
Closed Feb 5, 2014 at 10:57 PM by fcheung


willy562 wrote Dec 17, 2009 at 6:45 AM

I think it should return null, or at least expose another property to be able to know it has not been initialized.

I set it to () => null at the start of my application, so I can test it until the container is initialized.

Aesir wrote Jun 21, 2010 at 8:04 PM

I agree that it should return null or give me some way to know if ServiceLocator.Current is set. I have some applications that use an IOC container and some that do not yet use one. In a shared library I'd like to use the locator if it's available and use a default otherwise. Right now I have to use a exception handling for logic in my application which I'd rather avoid.

turtur wrote Mar 31, 2011 at 8:03 AM

Maybe add a property HasCurrent. Maybe throw InvalidOperationException when trying to fetch Current when no locator has been set.

thoughtcriminal wrote Oct 14, 2011 at 11:23 AM

It should return null, throwing exceptions is a bad idea in this case.
Catching NullReferenceException is not reliable (not to mention perf).

thoughtcriminal wrote Oct 14, 2011 at 11:26 AM

Can you please fix it as soon as possible? It's the highest voted issue and a change to return null would will not brake compatibility with the current version at all.

thoughtcriminal wrote Oct 14, 2011 at 11:32 AM

There has been a patch (5879) submitted jkitaaic on May 2 2010 which is being evaluated.
How long does it take to evaluate a patch at Microsoft?
Can you show us that you care about your .NET developer community and take care of this issue?

abatishchev wrote Jun 17, 2012 at 11:51 AM

That's annoying and ridiculous that Current throws exception instead of returning null on initial check.

fcheung wrote Feb 5, 2014 at 10:55 PM

We addressed this issue by adding an IsLocationProviderSet property to the static ServiceLocator class. If the LocationProvider is not set using the SetLocationProvider method, IsLocationProviderSet will return false and Current will throw an InvalidOperationException.

MichaelKetting wrote Feb 6, 2014 at 6:29 AM

Thank you for addressing this issue.

I do have a question in regard to the naming: The rest of the API on "ServiceLocator" is using "Locator" but the new API is using "Location". I would like to better understand the naming convention behind this.

Best regards, Michael

fcheung wrote Feb 6, 2014 at 4:24 PM

Hello Michael,

Looks like an oversight on my part. You're right that IsLocatorProviderSet is probably more consistent with other parts of the API. In my defense, however, the assembly is called "Microsoft.Practices.ServiceLocation".


MichaelKetting wrote Feb 7, 2014 at 6:07 AM

Hello Francis!

Thanks for clearing that one up.

Best regards, Michael