- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Unity 来管理应用服务器上的服务,但由于某种原因,我无法使用“GetAllInstances”方法。奇怪的是,相同类型的“GetInstance”似乎工作正常!
这是配置:
<alias alias="IService" type="Atom.Server.Infrastructure.Interface.Service.IService, Atom.Server.Infrastructure.Interface"/>
<alias alias="IAtomCommandService" type="Atom.CommandServer.AtomCommandService.Interface.IAtomCommandService, Atom.CommandServer.AtomCommandService.Interface"/>
<alias alias="AtomCommandService" type="Atom.CommandServer.AtomCommandService.AtomCommandService, Atom.CommandServer.AtomCommandService"/>
<register type="IService" mapTo="AtomCommandService">
<lifetime type="Singleton"/>
</register>
<register type="IAtomCommandService" mapTo="AtomCommandService">
<lifetime type="Singleton"/>
</register>
这个想法是,当服务器启动时,我需要能够获取所有已配置的 IService 实例来初始化它们。
IUnityContainer container = ConfigureUnityContainer();
UnityServiceLocator locator = new UnityServiceLocator(container);
var single = locator.GetInstance<IService>();
var all = locator.GetAllInstances<IService>().ToList();
正如我所说,single 可以工作,但 get all 不会返回任何内容。即使我从配置中删除 IAtomCommandService 映射并只使用 IService,它仍然无法工作。关于我哪里出错了有什么想法吗?
最佳答案
Unity 的工作方式是它只能接受给定抽象的一个未命名注册。 IIRC,如果您为同一接口(interface)注册另一个具体类型,则第二个将覆盖第一个。
因此,让多个服务实现相同类型的唯一方法是以不同的方式命名它们。尝试为每个 register
元素提供一个名称。
UnityContainer.ResolveAll 将返回所请求类型的所有命名注册,但不返回未命名注册(如果有的话)。
顺便说一句,不要使用 Service Locator anti-pattern .
关于.net - Unity 'GetAllInstances' 没有返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4689393/
我有一个根据命令修改记录的应用程序。我使用 StructureMap 作为我的容器,但它的行为并不符合我的预期。 该应用程序有多个命令来更新某些业务实体。我有许多 FluentValidation 验
我正在使用 Unity 来管理应用服务器上的服务,但由于某种原因,我无法使用“GetAllInstances”方法。奇怪的是,相同类型的“GetInstance”似乎工作正常! 这是配置:
我在最近的项目中实现事件处理时遇到了困难。 我已验证 structuremap 正在正确扫描、组装和添加 EventHandlers Scan(cfg => {
我正在使用 NInject 构建一个消息代理,我需要在容器中找到所有实现 Consumes 的实例,这是一个将类标记为能够使用特定消息类型的接口(interface)。 是否支持此场景? 最佳答案 内
我一直在尝试将 Caliburn.Micro MVVM 框架集成到我处于中间位置的 C# WPF 项目中。 我目前只有三个 View 模型: ShellViewModel -(带有 ContentCo
我曾在 Simple Injector 和 Caliburn micro 上工作过但已经有将近 2 年的时间了。今天,当我尝试创建一个简单的 WPF 应用程序时。首先,我最终阅读了文档,因为两个库都进
我是一名优秀的程序员,十分优秀!