Unity (w/ChildContainers) ignoring type mapping when WCF service request comes in?

Jun 21, 2010 at 2:02 PM

I'm using Unity with child containers - one main application-level container, and a child container created for each request.

A child container is being created by an HTTP Module for each request, and a WCF InstanceProvider is being used to instantiate the WCF service objects, so they can be built-up and injected.

When the application starts, a simple type mapping is registered - IPatientMapping to PatientMapping.

Immediately after that registration statement executes, I can successfully resolve it... no surprises there.

But, upon the first WCF service call- that same dependency fails to resolve, with the standard "ResolutionFailedException - cannot instantiate because it's an interface."

The strange thing is that the registration shows up shows up in the UnityContainer.Registrations collection while debugging, right before Unity blows up when trying to resolve that very same dependency.

Any ideas (ctavares maybe?) on why Unity might be "ignoring" or "forgetting" this registration? 

Like I said, it is still in the Registrations container - so it's not like everything was somehow lost between the app starting, and the first request coming in.  Also, it's not like the PatientMapping was never able to be resolved - it was successful in a test call to .Resolve right after the registration.

I've spent waay too much time on this, and am not looking forward to backing out all our projects form Unity and to another IoC container.  

Thanks for any suggestions,

Jun 21, 2010 at 9:48 PM
Edited Jun 21, 2010 at 9:48 PM

Does the WCF instance provider actually use the same container instance that's been configured? That'd be the first thing I'd check.


Jun 22, 2010 at 4:26 PM

Hi 'Chris'?

Thanks very much for the reply - yes, indeed it is the same container - We've been trying to figure this out for almost a week now.. I've been working with Unity for some time, so have an idea of the common pitfalls, or at least I thought I did.

Debugging/Quickwatching shows that the container cannot successfully resolve it when it gets to the WCF service instance provider (trying to do so throws the ResolutionFailedException noted above), however the registration is in the .Registrations collection of the UnityContainer object, even though it previously could do so immediately after the .Register call.

Reproducing the project structure (the assemblies being loaded dynamically into the AppDomain at runtime) in a simple test setup showed that nothing in that regard is causing the problem..

Jun 22, 2010 at 6:34 PM

Are you dynamically loading via Assembly.LoadFrom? If that's the case, you may be running into the "LoadFrom context". This results in two different copies of the same type in your appdomain, and the CLR doesn't consider them the same.

You might want to look at Suzanne Cook's blog for more details if that's what's going on.

Just guessing at this point.