2

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:

NullReferenceException on CreateChildContainer() call

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.

Nathan Friend
  • 12,155
  • 10
  • 75
  • 125
  • 1
    "Rerunning the application (without cleaning first)" cleaning recycles the ASP.NET worker. The rebuilding part is not key here. Killing w3wp.exe accomplishes the same thing. This looks like some state corruption. Could be a threading bug. Any threading concerns? – usr Nov 20 '15 at 21:23
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hamid Pourjam Nov 21 '15 at 11:37
  • This occured for me because I had a IHttpHandlerFactory that used the same container as the WebApi DependencyResolver. Once my IHttpHandlerFactory.ReleaseHandler() disposed the container, WebApi controllers started failing. The solution was to give the handler a separate container. Luckly, I didn't need any app-wide singletons or anything. – wtjones Sep 19 '16 at 14:43

1 Answers1

8

If you look at the code for UnityContainer..ctor(UnityContainer parent), the only place where a NullReferenceException could happen is on this line:

parent.lifetimeContainer.Add(this);

Which is inside a if (parent != null), so the only thing that can be null here is parent.lifetimeContainer. lifetimeContainer is initialized in InitializeBuilderState, which is called from the constructor, and the only other place where it's set is in the Dispose method.

So the error is likely because the parent container has already been disposed.

This is probably a bug in Unity; CreateChildContainer should check if the container has already been disposed, and throw a ObjectDisposedException if it has.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758