We have an application that uses Unity 2.1.505.0. Our application uses Web.API and a custom DependencyResolverAdapter
to enable dependency injection for our API controllers. Inside this adapter, we make a call to the CreateChildContainer()
method on the IUnityContainer
. However, this call intermittently throw a NullReferenceException
:
Under what circumstances does this method throw a NullReferenceException? The stack trace doesn't reveal any obvious cause:
at Microsoft.Practices.Unity.UnityContainer..ctor(UnityContainer parent)
at Microsoft.Practices.Unity.UnityContainer.CreateChildContainer()
at MyApp.DependencyResolverAdapter.BeginScope() in C:\ResCollection\CropProtection\MyApp\MyApp\DependencyResolverAdapter.cs:line 43
at System.Net.Http.HttpRequestMessageExtensions.GetDependencyScope(HttpRequestMessage request)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
The strange part is that after doing a "Clean Solution", the application works properly for about 20-30 seconds; controllers are properly resolved by Unity and the resolver seems to be doing its job. Then the NullReferenceException
is thrown, and no controllers are resolved successfully for the remainder of the application's lifetime. Rerunning the application (without cleaning first) results in this error immediately the first time a controller is resolved.