IOC StructureMap 中的 NServicebus 消息处理程序生命周期
如果我想要处理每个消息一个实例,我需要与 NServiceBus 消息处理程序结合使用所需的 IOC 实例生命周期是什么。
PerRequest 不起作用,因为考虑到我拥有大量的构造函数依赖项和依赖图,当我每次 Handle(MessageX msg) 调用只需要一个实例时,它会为我提供许多实例。我不能或不想将它直接注入到消息处理程序中,因为它需要在对象图的更下方。例如注入 IPersonService,依赖于 IPersonRepository,它们可以是每个请求(默认),但 IPersonDBContext 需要每个消息调用。
PerThread 将无法工作,因为 NServiceBus 一遍又一遍地使用相同的工作线程。
Singleton、HttpContext等显然不适用。
有人用 StructureMap 或 Castle 遇到过这个吗?
What is the required IOC instance lifecycle I need to use in conjuction with a NServiceBus message handler if I want an instance per message handled.
PerRequest won't work, since given the numerous constructor dependenices and dependency graph I have, it gives me many instances when I require only one per Handle(MessageX msg) call. I can't or don't want to inject it directly into the message handler since it is required further down the object graph. E.g. inject IPersonService, depends on IPersonRepository, they can be per request (Default), but IPersonDBContext need to be per message call.
PerThread won't work since NServiceBus uses the same worker threads over and over.
Singleton, HttpContext, etc.. are obviously not applicable.
Has anyone came across this with either StructureMap or Castle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能在这里遗漏了一些东西,但是 PerRequest 将为每个 MessageHandler 提供一个新实例(消息处理程序本身注册为 PerRequest)。我刚刚修复了一个错误,该错误导致消息处理程序为每条消息多次触发。我想知道这个 bug 是否误导了您(尝试从 CI 获取最新的 2.0 版本,看看是否适合您)
http://teamcity.codebetter.com/viewLog.html?buildId=7087&tab=artifacts&buildTypeId=bt96
希望这有帮助!
I might be missing something here, but PerRequest will give you a new instance for each MessageHandler ( the messagehandlers are them selves registered as PerRequest). I have just committed a fix for a bug that caused messagehandlers to fire multiple times for each message. I wonder if that bug has been misleading you (try to get the lastest 2.0 build from CI and see if that does it for you)
http://teamcity.codebetter.com/viewLog.html?buildId=7087&tab=artifacts&buildTypeId=bt96
Hope this helps!