C# unity 注册实例并解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2205611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unity register instance and resolve
提问by gavin stevens
I've written a class that has some dependencies it resolves from the unity container.
我写了一个类,它有一些从统一容器解析的依赖项。
From my main class I create a new object
从我的主类中,我创建了一个新对象
MyObject myObject = new MyObject();
I register it with my Unity Container
我用我的 Unity Container 注册它
UContainer.RegisterInstance<MyObject>(myObject, new ExternallyControlledLifetimeManager());
the I create the type that needs this as a dependency
我创建了需要它作为依赖项的类型
ConsumerObject consumer = new ConsumerObject();
the consumer looks like this:
消费者看起来像这样:
public class ConsumerObject
{
public ConsumberObject()
{
theObject = (MyObject)UContainer.Resolve(typeof(MyObject));
}
}
this throws an exception:
这会引发异常:
Resolution of the dependency failed, type = "MyObject", name = "". Exception message is: The current build operation (build key Build Key[MyObject, null]) failed: The parameter pp could not be resolved when attempting to call constructor MyObject(IPreferenceStorageProvider pp). (Strategy type BuildPlanStrategy, index 3)
依赖项解析失败,类型 = "MyObject",名称 = ""。异常消息是:当前构建操作(构建键 Build Key[MyObject, null])失败:尝试调用构造函数 MyObject(IPreferenceStorageProvider pp) 时无法解析参数 pp。(策略类型 BuildPlanStrategy,索引 3)
Why is my resolve call trying to call another contsructor on the type? I already created it and registered the instance.. I also tried it like: theObject = UContainer.Resolve<MyObject>();
doesn't seem to make any difference..
为什么我的解析调用试图调用该类型的另一个构造函数?我已经创建了它并注册了实例..我也试过了:theObject = UContainer.Resolve<MyObject>();
似乎没有任何区别..
Thanks
谢谢
回答by PanJanek
As far as I know Unity tries (by default) to call the constructor with largest number of parameters and tries to resolve each of the parameters from the mappings. You shold add the mapping for IPreferenceStorageProvider or remove the constructor that requires this parameter.
据我所知,Unity 尝试(默认情况下)调用具有最多参数的构造函数,并尝试从映射中解析每个参数。您应该为 IPreferenceStorageProvider 添加映射或删除需要此参数的构造函数。
If you don't want the IPreferenceStorageProvider parameter to by injected by unity maybe it shouldn't be declared as a constructor parameter at all. You can hard code instantiation of this object in default constructor.
如果您不希望通过统一注入 IPreferenceStorageProvider 参数,则可能根本不应将其声明为构造函数参数。您可以在默认构造函数中硬编码此对象的实例化。
回答by Darrel Miller
I'm not sure why you are seeing the behaviour you are. I just created a test that duplicated your scenario and it worked fine.
我不确定你为什么会看到你的行为。我刚刚创建了一个测试来复制您的场景,并且运行良好。
Have you tried something like this,
你有没有尝试过这样的事情,
public class ConsumerObject
{
public ConsumberObject(MyObject myObject)
{
theObject = myObject
}
}
and then using UContainer.Resolve<MyObject>()
?
然后使用UContainer.Resolve<MyObject>()
?
The only thing that I can think of is when you access UContainer.RegisterInstance and then UContainer.Resolve you are actually accessing two different containers. Could you show us how you are declaring UContainer?
我唯一能想到的是,当您访问 UContainer.RegisterInstance 和 UContainer.Resolve 时,您实际上是在访问两个不同的容器。你能告诉我们你是如何声明 UContainer 的吗?
回答by Yury Pekishev
I think the problem is that you using ExternallyControlledLifetimeManager. In this case Unity container holds only weak reference to your instance. And when you try to resolve, your instance already garbage collected. That's why the default LifeTimeManager for .RegisterInstance() is ContainerControlledLifeTimeManager. And Darrel Miller's case it works, because it not GC-ed yet. Try register your instance this way:
我认为问题在于您使用 ExternallyControlledLifetimeManager。在这种情况下,Unity 容器仅包含对您的实例的弱引用。当您尝试解析时,您的实例已被垃圾回收。这就是 .RegisterInstance() 的默认 LifeTimeManager 是 ContainerControlledLifeTimeManager 的原因。达雷尔·米勒 (Darrel Miller) 的案例很有效,因为它还没有经过 GC 处理。尝试以这种方式注册您的实例:
UContainer.RegisterInstance<MyObject>(myObject);
回答by Kálmán Bálint
"MyObject" is a type that implements an interface i presume. Try registering with the interface instead of the actual implementation.
“MyObject”是一种实现我认为的接口的类型。尝试注册接口而不是实际实现。
something like:
就像是:
UContainer.RegisterInstance<IMyObject>(myObject, new ExternallyControlledLifetimeManager());
Also if you have multiple implementation of the same interfeace, don't forget to name them upon registration, unless you want to override the already registered implementation.
此外,如果您有同一个接口的多个实现,请不要忘记在注册时命名它们,除非您想覆盖已经注册的实现。
For further help, i suggest you visit msdn:
如需进一步帮助,我建议您访问 msdn: