Unity - 依赖解析失败(未注册) [英] Unity - Resolution of the dependency failed (without registering)

查看:34
本文介绍了Unity - 依赖解析失败(未注册)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这行代码中遇到错误:

I'm getting an error on this line of code:

    using (IMaterialClient rawMaterialServiceProxy =
ServerUtility.Container.Resolve<IMaterialClient>())

错误:

依赖解析失败...当前类型,Xxx,是一个接口,不能构造.您是否缺少类型映射?

Resolution of the dependency failed... The current type, Xxx, is an interface and cannot be constructed. Are you missing a type mapping?

我没有注册具体的 IMaterialClient.在我刚刚观看的 Pluralsight 视频中,他们说您不必注册每种类型,因为如果没有指定,Unity 会找到一个实现.有没有改变?我错过了什么吗?怎么解决不了?运行此程序时,具有实际 IMaterialClient 实现的程序集位于 bin 文件夹中.

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.

推荐答案

如果他们这样说 Unity,那他们就错了.Unity 将解析具体类型 (.Resolve),但必须通过将接口与具体类型关联来显式注册接口.

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.

有诸如 Unity Auto Registration 之类的扩展来提供这些功能;我对他们没有经验.

There are extensions such as Unity Auto Registration to provide those features; I have no experience with them.

这篇关于Unity - 依赖解析失败(未注册)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆