问题
I'm getting an error on this line of code:
using (IMaterialClient rawMaterialServiceProxy =
ServerUtility.Container.Resolve<IMaterialClient>())
The error:
Resolution of the dependency failed... The current type, Xxx, is an interface and cannot be constructed. Are you missing a type mapping?
I'm not registering a concrete IMaterialClient. In the Pluralsight video I just watched, they said that you don't have to register every type because Unity will find an implementation if one wasn't specified. Has that changed? Am I missing something? Why won't that resolve? The assembly with the actual IMaterialClient implementation is in the bin folder when running this.
回答1:
If they said that about Unity, they're wrong. Unity will resolve a concrete type (.Resolve<MyClass>), but interfaces have to be explicitly registered by associating them with concrete types.
There are extensions such as Unity Auto Registration to provide those features; I have no experience with them.
回答2:
I'm not aware of that feature in Unity. As far as I know, it will happily resolve unregistered concrete types, but has to have had a concrete type registered for any abstract types or interfaces. Best bet is to register it:
ServerUtility.Container.RegisterType<IMaterialClient, ConcreteMaterialClient>();
来源:https://stackoverflow.com/questions/13752427/unity-resolution-of-the-dependency-failed-without-registering