如何在运行时扩展 Unity 目录?
我想将大部分接口映射到 app.config 文件中的具体类。但是,我想在运行时将一些接口注册到同一个 Unity 目录。我尝试了下面的代码,但它给了我一个SynchronizationLockException:从不同步的代码块调用了对象同步方法。
IUnityContainer container = new UnityContainer();
UnityConfigurationSection configSection =
(UnityConfigurationSection)ConfigurationManager.GetSection("unity");
configSection.Containers.Default.Configure(container);
container.RegisterInstance<IInterface>(new ConcreteObject());
如何在Unity中在运行时注册对象从app.config初始化目录?
我使用的是 Prism4 附带的 Unity 版本 (2.0)。
I want to map most of my interfaces to concrete classes in my app.config file. However, I would like to register some interfaces to the same Unity catalog at runtime. I tried the code below, but it gives me a SynchronizationLockException: Object synchronization method was called from an unsynchronized block of code.
IUnityContainer container = new UnityContainer();
UnityConfigurationSection configSection =
(UnityConfigurationSection)ConfigurationManager.GetSection("unity");
configSection.Containers.Default.Configure(container);
container.RegisterInstance<IInterface>(new ConcreteObject());
How can I register an object at runtime in a Unity catalog initialized from app.config?
I am using the Unity version (2.0) that ships with Prism4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是注册 Unity 对象时的一个常见问题,但从技术上来说并不是一个错误。将您的注册更改为:
这应该可以解决您的问题。
由于以下原因引发该异常:
Can Unity是否总是抛出 SynchronizationLockException?
This is a common problem, but not technically an error, with registering unity objects. Change your regsitration to this:
That should fix your problem.
That exception gets thrown because of this:
Can Unity be made to not throw SynchronizationLockException all the time?