服务脚本示例

适用于:SQL ServerAzure SQL 托管实例

此 Transact-SQL 代码示例定义用于存档非类型化的 XML 文档的服务。 包括两个脚本:协定脚本和服务定义脚本。 约定脚本定义消息类型和服务的约定。 消息类型定义和协定定义应同时匹配发起服务和目标服务。 因此,定义包含在单独的服务定义脚本中,该脚本可以分发到托管发起服务的数据库。 服务定义脚本定义服务本身。 此脚本只能在实现目标服务的数据库中运行。

注意

服务定义脚本定义目标服务,但不包括服务的实现。

合约脚本

注意

本文中的代码示例是使用 AdventureWorks2025 示例数据库进行测试的,可以从 Microsoft SQL Server 示例和社区项目 主页下载该数据库。

-- The contract script contains definitions that must be present
-- for both the initiating service and the target service.
USE AdventureWorks2008R2;
GO

-- Create messages for each broker-to-broker
-- communication needed to complete the task.

-- Message for the initiator to send XML
-- to be archived.
CREATE MESSAGE TYPE [//Adventure-Works.com/messages/ArchiveXML]
    VALIDATION = WELL_FORMED_XML;
GO

-- Message to return event archiving information.
CREATE MESSAGE TYPE [//Adventure-Works.com/messages/AcknowledgeArchiveXML]
    VALIDATION = WELL_FORMED_XML;
GO

-- Create a service contract to structure
-- an event archiving conversation, using
-- the message types defined above.
CREATE CONTRACT [//Adventure-Works.com/contracts/ArchiveXML/v1.0]
    ([//Adventure-Works.com/messages/ArchiveXML] SENT BY INITIATOR,
    [//Adventure-Works.com/messages/AcknowledgeArchiveXML] SENT BY TARGET);
GO

服务定义脚本

-- This script defines the target service. The objects created
-- by this script are only required in a database that hosts
-- the target service.
USE AdventureWorks2008R2;
GO

-- Create the service queue that will receive
-- messages for conversations that implement
-- the ArchiveXML contract.
CREATE QUEUE ArchiveQueue;
GO

-- Create the service object that exposes the
-- ArchiveEvents service contract and maps
-- it to the ArchiveQueue service queue.
CREATE SERVICE [//Adventure-Works.com/ArchiveService]
    ON QUEUE ArchiveQueue
    ([//Adventure-Works.com/contracts/ArchiveXML/v1.0]);
GO