ASP.NET 应用程序可以处理 NServiceBus 事件吗?
大多数(如果不是全部)ASP.NET(或 MVC)的 NSB 示例都有 Web 应用程序使用 Bus.Send 发送消息,并可能注册一个简单的回调,这基本上就是我使用的方式它在我的应用程序中。
我想知道在同一个 ASP.NET 应用程序中处理消息是否可能和/或有意义。
我问的主要原因是缓存。该过程可能如下所示:
- 用户从 Web 应用程序发起请求。
- Web 应用程序向独立应用程序服务器发送消息,并将更改记录在本地数据库中。
- 在来自同一用户的未来页面请求中,Web 应用程序会意识到更改并将其列为“待处理”状态。
- 后端会发生很多事情,最终请求会被批准或拒绝。引用原始请求的事件已发布。
- 此时,网络应用应该开始显示最新信息。
现在,在真正的网络应用程序中,几乎可以肯定的是,这个挂起的请求将被缓存,很可能会缓存很长一段时间,因为否则应用程序必须每次查询数据库以获取挂起的更改/em> 用户询问当前信息。
因此,当请求最终在后端完成时(可能需要一分钟或一天),Web 应用程序至少需要使该缓存条目无效并进行另一次数据库查找。
现在我意识到这可以通过 SqlDependency 对象等进行管理,但我们假设它们不可用 - 也许它不是 SQL Server 后端,或者当前信息查询可能会转到网络服务,无论如何。问题是,Web 应用程序如何意识到状态的变化?
如果可以在 ASP.NET 应用程序中处理 NServiceBus 消息,那么处理程序的上下文是什么?换句话说,IoC 容器将不得不注入一堆依赖项,但它们的范围是什么?这一切都是在 HTTP 请求的上下文中执行的吗?或者消息处理程序的所有内容都需要是静态/单例的吗?
对于此类问题是否有更好/推荐的方法?
Most if not all of the NSB examples for ASP.NET (or MVC) have the web application sending a message using Bus.Send
and possibly registering for a simple callback, which is essentially how I'm using it in my application.
What I'm wondering is if it's possible and/or makes any sense to handle messages in the same ASP.NET application.
The main reason I'm asking is caching. The process might go something like this:
- User initiates a request from the web app.
- Web app sends a message to a standalone app server, and logs the change in a local database.
- On future page requests from the same user, the web app is aware of the change and lists it in a "pending" status.
- A bunch of stuff happens on the back-end and eventually the requests gets approved or rejected. An event is published referencing the original request.
- At this point, the web app should start displaying the most recent information.
Now, in a real web app, it's almost a sure thing that this pending request is going to be cached, quite possibly for a long period of time, because otherwise the app has to query the database for pending changes every time the user asks for the current info.
So when the request finally completes on the back-end - which might take a minute or a day - the web app needs, at a minimum, to invalidate this cache entry and do another DB lookup.
Now I realize that this can be managed with SqlDependency
objects and so on, but let's assume that they aren't available - perhaps it's not a SQL Server back-end or perhaps the current-info query goes to a web service, whatever. The question is, how does the web app become aware of the change in status?
If it is possible to handle NServiceBus messages in an ASP.NET application, what is the context of the handler? In other words, the IoC container is going to have to inject a bunch of dependencies, but what is their scope? Does this all execute in the context of an HTTP request? Or does everything need to be static/singleton for the message handler?
Is there a better/recommended approach to this type of problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己也想知道同样的事情 - Web 应用程序与 NServiceBus 基础设施的适当耦合级别是多少?在我的领域,我有一个类似的问题需要解决,涉及使用 SignalR 代替缓存。和你一样,我没有找到很多关于这个特定模式的文档。然而,我认为可以推理出遵循它的一些含义,然后决定它在您的环境中是否有意义。
简而言之,我想说我相信 Web 应用程序订阅 NServiceBus 事件是完全可能的。我不认为会有任何技术障碍,尽管我必须承认我还没有真正尝试过 - 如果你有时间,请务必尝试一下。我有一种强烈的感觉,如果有人开始需要这样做,那么可能会有更好的整体设计等待被发现。这就是为什么我认为是这样的:
这有助于澄清事情吗?
I've wondered the same thing myself - what's an appropriate level of coupling for a web app with the NServiceBus infrastructure? In my domain, I have a similar problem to solve involving the use of SignalR in place of a cache. Like you, I've not found a lot of documentation about this particular pattern. However, I think it's possible to reason through some of the implications of following it, then decide if it makes sense in your environment.
In short, I would say that I believe it is entirely possible to have a web application subscribe to NServiceBus events. I don't think there would be any technical roadblocks, though I have to confess I have not actually tried it - if you have the time, by all means give it a shot. I just get the strong feeling that if one starts needing to do this, then there is probably a better overall design waiting to be discovered. Here's why I think this is so:
Does this help clarify things?
可以创建一个端点(NSB)来订阅已发布的事件并更新缓存。在实际更新完成之前不应发布该活动,这样您就不会失去同步。 Web 应用程序将在下一个请求时继续从缓存中提取数据,或者您可以构建某种延迟。
An endpoint(NSB) can be created to subscribe to the published event and update the cache. The event shouldn't be published until the actual update is made so you don't get out of sync. The web app would continue to pull data from the cache on the next request, or you can build in some kind of delay.