简体   繁体   English

你/你应该在.NET应用程序中使用SQL Server Service Broker吗?

[英]Can you/should you use SQL Server Service Broker with .NET applications?

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. 目前我正在使用数据库轮询,但我听说SQL Server Service Broker可以为我提供类似MSMQ的功能。

  1. Can I listen to SQL Server Service Broker queues from .NET applications running on a different machine? 我可以从在不同计算机上运行的.NET应用程序中侦听SQL Server Service Broker队列吗?
  2. If so, should I do it? 如果是的话,我应该这样做吗?
  3. If not, what would you recommend? 如果没有,你会推荐什么?

SSB (SQL Service Broker) has a feature named Activation that enabled a stored procedure to be attached to a queue. SSB(SQL Service Broker)具有一个名为Activation的功能,可以将存储过程附加到队列。 SQL Server will run this procedure internally when there are messages to consume in a queue. 当队列中有消息消息时,SQL Server将在内部运行此过程。 The queue attached procedure can be a CLR procedure, enabling for managed code business logic modules to run (C#, VB.Net etC). 队列附加过程可以是CLR过程,使托管代码业务逻辑模块能够运行(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. 内部激活的存储过程的替代方法是让外部客户端使用WAITFOR(RECEIVE ... )语句“侦听”队列。 This syntax is special for SSB and does a non-pooling block until there are messages to receive. 此语法对于SSB是特殊的,并且在有消息要接收之前执行非池化块。 Applications then consume the received messages as an ordinary T-SQL result set (like a SELECT). 然后,应用程序将接收的消息作为普通的T-SQL结果集(如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. 还有一个Service Broker外部激活器示例,它利用事件通知机制知道何时启动应用程序以消耗队列中的消息。

If you want to see a sample of T-SQL code that leverages SSB internal Activation check out Asynchronous procedure execution . 如果要查看利用SSB内部激活的T-SQL代码示例,请检查异步过程执行

To answer your questions: 回答你的问题:

Can I listen to SQL Server Service Broker queues from .NET applications running on a different machine? 我可以从在不同计算机上运行的.NET应用程序中侦听SQL Server Service Broker队列吗?

Yes. 是。

If so, should I do it? 如果是的话,我应该这样做吗?

If not, what would you recommend? 如果没有,你会推荐什么?

You might consider using SqlDependency . 您可以考虑使用SqlDependency It uses Service Broker behind the scenes, but not explicitly. 它在幕后使用Service Broker,但没有明确说明。

You can register a SqlDependency object with a SELECT query or a stored procedure. 您可以使用SELECT查询或存储过程注册SqlDependency对象。 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 use SqlCacheDependency , which will just remove the associated object from the Cache when the event fires. 或者,您可以使用SqlCacheDependency ,它将在事件触发时从Cache中删除关联的对象。

You can also use Service Broker directly. 您也可以直接使用Service Broker。 However, in that case you will need to send and receive your own messages, as with MSMQ. 但是,在这种情况下,您需要发送和接收自己的消息,就像使用MSMQ一样。

In load-balanced environments, SqlDependency is good for cases where code needs to run on every web server (such as flushing the cache). 在负载平衡的环境中, SqlDependency适用于需要在每个Web服务器上运行代码的情况(例如刷新缓存)。 Service Broker messages are better for code than should only run once -- such as sending an email. Service Broker消息对于代码来说比仅运行一次更好 - 例如发送电子邮件。

In case it helps, I cover both systems in detail with examples in my book ( Ultra-Fast ASP.NET ). 如果它有帮助,我会在我的书( Ultra-Fast ASP.NET )中使用示例详细介绍这两个系统。

A easy to use queue library for SQL Service Broker based on rhino-queues 一个易于使用的基于rhino-queues的SQL Service Broker队列库

http://github.com/CoreyKaylor/servicebroker-queues http://github.com/CoreyKaylor/servicebroker-queues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你能在WPF应用程序中使用jQuery吗? - Can you use jQuery in WPF Applications? 为什么要在 SQL Server 中使用 .net 程序集? - Why would you want to use a .net assembly in SQL Server? 使用SQL Server Service Broker在.NET应用之间进行消息传递 - Using SQL Server Service Broker for messaging between .NET apps SQL Server 中的 Service Broker 是什么? - What is Service Broker in SQL Server? SQL Server Service Broker问题和教程 - SQL Server Service Broker Issue & Tutorials 您可以将Web服务添加到现有Windows服务项目(.NET)吗? - Can you add a web service to an existing Windows Service project (.NET)? 即使您在应用程序中以.net 2为目标,我在设计时仍可以使用Linq to SQL吗? - Is it true that I can still use Linq to SQL in design time even if you are targeting .net 2 in your application? SQL Server for .Net应用程序的替代方案 - Alternative of SQL Server for .Net applications 您如何判断请求是否应呈现在服务器端? (ReactJS) - How can you tell if a request should render server side? (ReactJS) 为什么/什么时候应该在 .net 中使用嵌套类? 或者你不应该? - Why/when should you use nested classes in .net? Or shouldn't you?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM