8

我想实现一个调用 Web 服务的存储过程(在服务代理基础设施内)。我查看了 Aschenbrenner 关于 Service Broker 的书中的一些示例。但是,我没有找到任何带有 Web 服务调用的内容。有人可以帮忙吗?

感谢 Sqlbs

4

3 回答 3

4

我们在我的公司有一个类似的任务,并想出一个最佳解决方案是使用带有外部激活器的异步触发器,它从 .NET 调用 web 服务并在成功调用后将消息出队。这意味着您创建了一个常规数据库触发器,该触发器将消息发送到服务代理队列以进行异步处理。AKA 异步触发器。这是克劳斯的书第 10 章的一个样本

-- Create the trigger written with T-SQL
CREATE TRIGGER OnCustomerInserted ON Customers FOR INSERT
AS
DECLARE @conversationHandle UNIQUEIDENTIFIER
DECLARE @fromService SYSNAME
DECLARE @toService SYSNAME
DECLARE @onContract SYSNAME
DECLARE @messageBody XML

SET @fromService = 'CustomerInsertedClient'
SET @toService = 'CustomerInsertedService'
SET @onContract = 'http://ssb.csharp.at/SSB_Book/c10/CustomerInsertContract'

-- Check if there is already an ongoing conversation with the TargetService
SELECT @conversationHandle = ConversationHandle FROM SessionConversations
    WHERE SPID = @@SPID
    AND FromService = @fromService
    AND ToService = @toService
    AND OnContract = @onContract

IF @conversationHandle IS NULL
BEGIN
    -- We have to begin a new Service Broker conversation with the TargetService
    BEGIN DIALOG CONVERSATION @conversationHandle
        FROM SERVICE @fromService
        TO SERVICE @toService
        ON CONTRACT @onContract
        WITH ENCRYPTION = OFF;

    -- Create the dialog timer for ending the ongoing conversation
    BEGIN CONVERSATION TIMER (@conversationHandle) TIMEOUT = 5;

    -- Store the ongoing conversation for further use
    INSERT INTO SessionConversations (SPID, FromService, ToService, OnContract, ConversationHandle)
    VALUES
    (
        @@SPID,
        @fromService,
        @toService,
        @onContract,
        @conversationHandle
    )
END

-- Construct the request message
SET @messageBody = (SELECT * FROM INSERTED FOR XML AUTO, ELEMENTS);

-- Send the message to the TargetService
;SEND ON CONVERSATION @conversationHandle
MESSAGE TYPE [http://ssb.csharp.at/SSB_Book/c10/CustomerInsertedRequestMessage] (@messageBody);

与其使用通过托管代码(内部激活)调用 Web 服务的存储过程,我们决定最好将处理卸载到 sql server 之外。并发现了这个由 Microsoft 创建的不错的小工具 - External Activator ,它将监听激活队列并在队列中有新消息时启动应用程序。具体实现请参考本书中克劳斯的第 4 章。

于 2012-06-21T22:20:44.833 回答
0

我会制作位于服务代理末端的 Windows 服务(并像在任何 Win 应用程序中一样调用 Web 服务)。不知何故,不要认为从 db 调用 Web 服务是个好主意。

可以在这里找到外部激活器。并在此处下载服务代理接口/外部激活器。服务代理界面很棒!便于使用。

于 2012-01-24T08:12:23.463 回答
-1

请参阅第 10 章中的第一个示例。如果您的问题是关于实现 Web 服务调用的详细信息,请使用适当的 Web 服务标签而不是服务代理来标记问题。

于 2010-08-11T20:20:22.453 回答