Unity - Resolution of the dependency failed (without registering)

£可爱£侵袭症+ 提交于 2019-12-07 13:10:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!