您可以/应该将 SQL Server Service Broker 与 .NET 应用程序一起使用吗?
我在数据库中有很多操作需要触发应用程序代码。目前我正在使用数据库轮询,但我听说 SQL Server Service Broker 可以为我提供类似 MSMQ 的功能。
- 我可以从运行在不同计算机上的 .NET 应用程序侦听 SQL Server Service Broker 队列吗?
- 如果是这样,我应该这样做吗?
- 如果没有,你会推荐什么?
I have many operations in the database that need to trigger application code. Currently I am using database polling, but I hear that SQL Server Service Broker can give me MSMQ-like functionality.
- Can I listen to SQL Server Service Broker queues from .NET applications running on a different machine?
- If so, should I do it?
- If not, what would you recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SSB (SQL Service Broker) 有一个名为 激活 的功能 使存储过程能够附加到队列。当队列中有消息要使用时,SQL Server 将在内部运行此过程。队列附加过程可以是 CLR 过程,允许托管代码业务逻辑模块运行(C#、VB.Net 等)。
内部激活的存储过程的替代方法是让外部客户端使用
WAITFOR(RECEIVE ... )
语句“侦听”队列。此语法对于 SSB 来说是特殊的,并且会执行非池化块,直到有消息要接收为止。然后,应用程序将接收到的消息用作普通的 T-SQL 结果集(如 SELECT)。还有一个 的示例Service Broker 的外部激活器,利用事件通知机制来了解何时启动应用程序以使用队列中的消息。如果您想查看利用 SSB 内部激活的 T-SQL 代码示例,请查看 异步过程执行。
SSB (SQL Service Broker) has a feature named Activation that enabled a stored procedure to be attached to a queue. SQL Server will run this procedure internally when there are messages to consume in a queue. The queue attached procedure can be a CLR procedure, enabling for managed code business logic modules to run (C#, VB.Net etC).
An alternative to an internal activated stored procedure is to have an external client 'listen' on a queue with a
WAITFOR(RECEIVE ... )
statement. This syntax is special for SSB and does a non-pooling block until there are messages to receive. Applications then consume the received messages as an ordinary T-SQL result set (like a SELECT). There is also a sample of an External Activator for Service Broker that leverages the event notification mechanism to know when to start an application for consuming messages from a queue.If you want to see a sample of T-SQL code that leverages SSB internal Activation check out Asynchronous procedure execution.
回答您的问题:
是的。
您可以考虑使用
SqlDependency
。它在幕后使用 Service Broker,但不明确使用。您可以使用
SELECT
查询或存储过程注册SqlDependency
对象。如果另一个命令更改了从查询返回的数据,则会触发一个事件。您可以注册一个事件处理程序,然后运行您当时喜欢的任何代码。或者,您可以使用SqlCacheDependency
,它会在事件触发时从缓存中删除关联的对象。您也可以直接使用 Service Broker。但是,在这种情况下,您将需要发送和接收自己的消息,就像使用 MSMQ 一样。
在负载平衡环境中,SqlDependency 非常适合代码需要在每个 Web 服务器上运行的情况(例如刷新缓存)。 Service Broker 消息对于代码来说比只运行一次更好——例如发送电子邮件。
如果有帮助的话,我会在书中详细介绍这两个系统(超快速 ASP .NET)。
To answer your questions:
Yes.
You might consider using
SqlDependency
. It uses Service Broker behind the scenes, but not explicitly.You can register a
SqlDependency
object with aSELECT
query or a stored procedure. If another command changes the data that was returned from the query, then an event will fire. You can register an event handler, and run whatever code you like at that time. Or, you can useSqlCacheDependency
, which will just remove the associated object from the Cache when the event fires.You can also use Service Broker directly. However, in that case you will need to send and receive your own messages, as with MSMQ.
In load-balanced environments,
SqlDependency
is good for cases where code needs to run on every web server (such as flushing the cache). Service Broker messages are better for code than should only run once -- such as sending an email.In case it helps, I cover both systems in detail with examples in my book (Ultra-Fast ASP.NET).
一个易于使用的基于 rhino-queues 的 SQL Service Broker 队列库
http://github.com/CoreyKaylor /servicebroker-queues
A easy to use queue library for SQL Service Broker based on rhino-queues
http://github.com/CoreyKaylor/servicebroker-queues