IOC StructureMap 中的 NServicebus 消息处理程序生命周期

发布于 2024-08-10 20:09:46 字数 421 浏览 4 评论 0原文

如果我想要处理每个消息一个实例,我需要与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮湿 2024-08-17 20:09:46

我可能在这里遗漏了一些东西,但是 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文