Search code examples
wcfinterfaceunity-containerresolve

Unity Exception resolving type that is correctly mapped. Why?


I'm getting this error trying to implement the sample WCF Dependency Injection with Unity.

Resolution of the dependency failed, type = "SampleServiceUsingUnity.ExampleService", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type,    ExampleServiceUsingUnity.IExampleManager, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving SampleServiceUsingUnity.ExampleService,(none)
Resolving parameter "exampleManager" of constructor     SampleServiceUsingUnity.ExampleService(SampleServiceUsingUnity.IExampleManager exampleManager)
Resolving SampleServiceUsingUnity.IExampleManager,(none)

Problem is, I've tried to even explicitly map the types...and when I debug the code I go to the immediate window and look at the registrations. See below:

**DependencyFactory.Container.Registrations.ToList()[2]**

{Microsoft.Practices.Unity.ContainerRegistration}
buildKey: {Build Key[SampleServiceUsingUnity.IExampleRepository, null]}
LifetimeManager: {Microsoft.Practices.Unity.ContainerControlledLifetimeManager}
LifetimeManagerType: {Name = "ContainerControlledLifetimeManager" FullName = "Microsoft.Practices.Unity.ContainerControlledLifetimeManager"}
MappedToType: {Name = "ExampleRepository" FullName = "SampleServiceUsingUnity.ExampleRepository"}
Name: null
RegisteredType: {Name = "IExampleRepository" FullName = "SampleServiceUsingUnity.IExampleRepository"}

So the mapping looks right, but when the service attempts to instantiate that interface using Resolve, I get the error.

The code examples are supplied at: http://msdn.microsoft.com/en-us/library/hh323725.aspx


Solution

  • The exception is talking about IExampleManager while the registrations are about IExampleRepository. Any chance you are missing the mapping for the manager and instead supplied one for the repository?