你能/应该使用SQL Server服务代理与.NET应用程序?你能、应用程序、Server、SQL

2023-09-03 09:03:17 作者:高冷型男

我在需要触发的应用code数据库的许多操作。目前我使用的数据库查询,但我听到的SQL Server服务中介可以给我MSMQ一样的功能。

我可以倾听来自不同的机器?在运行的.NET应用程序的SQL Server Service Broker的队列 如果是这样,我该怎么办呢? 如果没有,你有什么建议? 解决方案

要回答你的问题:

  

我可以听的SQL Server服务   从.NET应用程序代理队列   在不同的机器上运行?

是的。

  

如果是这样,我该怎么办呢?

     SQL Server免费版的安装以及使用SQL Server Management Studio SSMS 连接数据库的图文方法

如果没有,你有什么建议?

您可以考虑使用的SqlDependency 。它使用服务代理在幕后,但没有明确。

您可以注册一个 SELECT 查询或存储过程的的SqlDependency 对象。如果另一个命令改变从查询返回的数据,那么事件将触发。你可以注册一个事件处理程序,并运行任何code你想在那个时候。或者,你可以使用的SqlCacheDependency ,这只是从缓存中删除关联对象时,事件触发。

您也可以使用服务代理直接。然而,在这种情况下,你将需要发送和接收自己的消息,与MSMQ。

在负载平衡的环境中,的SqlDependency 是良好的,其中code需要每个Web服务器上运行的情况下(如清空缓存)。 Service Broker消息是更好地为code比应该只运行一次 - 如发送电子邮件

在情况下,它可以帮助,我将介绍这两个系统在细节,在我的书的例子(超快速ASP.NET )。

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?

解决方案

To answer your questions:

Can I listen to SQL Server Service Broker queues from .NET applications running on a different machine?

Yes.

If so, should I do it?

If not, what would you recommend?

You might consider using SqlDependency. It uses Service Broker behind the scenes, but not explicitly.

You can register a SqlDependency object with a SELECT 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 use SqlCacheDependency, 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).