--======================================================================================================================
--Step 1
--======================================================================================================================
--========================================================================
--Create endpoint with windows authentication
--========================================================================
USE master;
GO
IF EXISTS (SELECT * FROM master.sys.endpoints
WHERE name = N'InstTargetEndpoint')
DROP ENDPOINT InstTargetEndpoint;
GO
CREATE ENDPOINT InstTargetEndpoint
STATE = STARTED
AS TCP ( LISTENER_PORT = 4022 )
FOR SERVICE_BROKER (AUTHENTICATION = WINDOWS );
GO
--========================================================================
--Create traget database InstTargetDB
--========================================================================
USE master;
GO
IF EXISTS (SELECT * FROM sys.databases
WHERE name = N'InstTargetDB')
DROP DATABASE InstTargetDB;
GO
CREATE DATABASE InstTargetDB;
GO
USE InstTargetDB;
GO
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = N'Auto@sql';
GO
CREATE USER TargetUser WITHOUT LOGIN;
GO
--======================================================================================
--Create certification and backup it
--======================================================================================
USE InstTargetDB
GO
CREATE CERTIFICATE InstTargetCertificate
AUTHORIZATION TargetUser
WITH SUBJECT = 'Target Certificate',
EXPIRY_DATE = N'12/31/2012';
BACKUP CERTIFICATE InstTargetCertificate
TO FILE =
N'\\Ms-wengao-02\cert\InstTargetCertificate.cer';
GO
--======================================================================================
--Create message type,contract,queue,services
--======================================================================================
USE InstTargetDB
GO
CREATE MESSAGE TYPE [//BothDB/2InstSample/RequestMessage]
VALIDATION = WELL_FORMED_XML;
CREATE MESSAGE TYPE [//BothDB/2InstSample/ReplyMessage]
VALIDATION = WELL_FORMED_XML;
GO
CREATE CONTRACT [//BothDB/2InstSample/SimpleContract]
([//BothDB/2InstSample/RequestMessage]
SENT BY INITIATOR,
[//BothDB/2InstSample/ReplyMessage]
SENT BY TARGET
);
GO
CREATE QUEUE InstTargetQueue;
GO
CREATE SERVICE [//TgtDB/2InstSample/TargetService]
AUTHORIZATION TargetUser
ON QUEUE InstTargetQueue
([//BothDB/2InstSample/SimpleContract]);
GO
--======================================================================================================================
--Step 3
--======================================================================================================================
--======================================================================================
--Create message type,contract,queue,services
--======================================================================================
USE InstTargetDB
GO
CREATE USER InitiatorUser WITHOUT LOGIN;
CREATE CERTIFICATE InstInitiatorCertificate
AUTHORIZATION InitiatorUser
FROM FILE =
N'\\Ms-wengao-02\cert\InstInitiatorCertificate.cer';
GO
USE InstTargetDB;
GO
ALTER ROUTE InstInitiatorRoute
WITH SERVICE_NAME = N'//InstDB/2InstSample/InitiatorService',
ADDRESS = N'TCP://172.22.101.96:4022';
USE msdb
GO
ALTER ROUTE InstTargetRoute
WITH SERVICE_NAME = N'//TgtDB/2InstSample/TargetService',
ADDRESS =N'TCP://172.22.101.214:4022';
GO
GRANT SEND ON SERVICE::[//TgtDB/2InstSample/TargetService]
TO InitiatorUser;
GO
CREATE REMOTE SERVICE BINDING InitiatorBinding
TO SERVICE N'//InstDB/2InstSample/InitiatorService'
WITH USER = InitiatorUser;
GO
--======================================================================================================================
--Step 5
--======================================================================================================================
USE InstTargetDB
GO
SELECT * FROM dbo.InstTargetQueue
GO
DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
DECLARE @RecvReqMsg NVARCHAR(100);
DECLARE @RecvReqMsgName sysname;
BEGIN TRANSACTION;
WAITFOR
( RECEIVE TOP(1)
@RecvReqDlgHandle = conversation_handle,
@RecvReqMsg = message_body,
@RecvReqMsgName = message_type_name
FROM InstTargetQueue
), TIMEOUT 1000;
SELECT @RecvReqMsg AS ReceivedRequestMsg;
IF @RecvReqMsgName = N'//BothDB/2InstSample/RequestMessage'
BEGIN
DECLARE @ReplyMsg NVARCHAR(100);
SELECT @ReplyMsg =
N'<ReplyMsg>Message for Initiator service.</ReplyMsg>';
SEND ON CONVERSATION @RecvReqDlgHandle
MESSAGE TYPE [//BothDB/2InstSample/ReplyMessage]
(@ReplyMsg);
END CONVERSATION @RecvReqDlgHandle;
END
SELECT @ReplyMsg AS SentReplyMsg;
SELECT @ReplyMsg;
COMMIT TRANSACTION;
GO
--======================================================================================================================
--Step 2
--======================================================================================================================
--========================================================================
--Create endpoint with windows authentication
--========================================================================
USE master;
GO
IF EXISTS (SELECT * FROM sys.endpoints
WHERE name = N'InstInitiatorEndpoint')
DROP ENDPOINT InstInitiatorEndpoint;
GO
CREATE ENDPOINT InstInitiatorEndpoint
STATE = STARTED
AS TCP ( LISTENER_PORT = 4022 )
FOR SERVICE_BROKER (AUTHENTICATION = WINDOWS );
GO
--========================================================================
--Create Initiator database InstInitiatorDB
--========================================================================
USE master;
GO
IF EXISTS (SELECT * FROM sys.databases
WHERE name = N'InstTargetDB')
DROP DATABASE InstInitiatorDB;
GO
CREATE DATABASE InstInitiatorDB;
GO
USE InstInitiatorDB;
GO
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = N'Auto@sql';
GO
CREATE USER InitiatorUser WITHOUT LOGIN;
GO
--======================================================================================
--Create certification and backup it
--======================================================================================
USE InstInitiatorDB
GO
CREATE CERTIFICATE InstInitiatorCertificate
AUTHORIZATION InitiatorUser
WITH SUBJECT = 'Target Certificate',
EXPIRY_DATE = N'12/31/2012';
GO
BACKUP CERTIFICATE InstInitiatorCertificate
TO FILE =
N'\\Ms-wengao-02\cert\InstInitiatorCertificate.cer';
GO
--======================================================================================
--Create message type,contract,queue,services
--======================================================================================
USE InstInitiatorDB
GO
CREATE MESSAGE TYPE [//BothDB/2InstSample/RequestMessage]
VALIDATION = WELL_FORMED_XML;
CREATE MESSAGE TYPE [//BothDB/2InstSample/ReplyMessage]
VALIDATION = WELL_FORMED_XML;
GO
CREATE CONTRACT [//BothDB/2InstSample/SimpleContract]
([//BothDB/2InstSample/RequestMessage]
SENT BY INITIATOR,
[//BothDB/2InstSample/ReplyMessage]
SENT BY TARGET
);
GO
CREATE QUEUE InstInitiatorQueue;
GO
CREATE SERVICE [//InstDB/2InstSample/InitiatorService]
AUTHORIZATION InitiatorUser
ON QUEUE InstInitiatorQueue;
GO
--======================================================================================
--Create user TargetUser
--Use the certificate which create by traget to Create cetificate
--======================================================================================
USE InstInitiatorDB
CREATE USER TargetUser WITHOUT LOGIN;
GO
CREATE CERTIFICATE InstTargetCertificate
AUTHORIZATION TargetUser
FROM FILE =
N'\\Ms-wengao-02\cert\InstTargetCertificate.cer'
GO
--======================================================================================
--Create route and remote service binding
--======================================================================================
USE InstInitiatorDB
GO
CREATE ROUTE InstTargetRoute
WITH SERVICE_NAME = N'//TgtDB/2InstSample/TargetService',
ADDRESS = 'TCP://172.22.101.214:4022';
USE msdb;
GO
CREATE ROUTE InstInitiatorRoute
WITH SERVICE_NAME = N'//InstDB/2InstSample/InitiatorService',
ADDRESS = N'TCP://172.22.101.96:4022';
GO
CREATE REMOTE SERVICE BINDING TargetBinding
TO SERVICE N'//TgtDB/2InstSample/TargetService'
WITH USER = TargetUser;
GO
--======================================================================================================================
--Step 4
--======================================================================================================================
--======================================================================================
--Start conversation
--======================================================================================
USE InstInitiatorDB;
GO
DECLARE @InitDlgHandle UNIQUEIDENTIFIER;
DECLARE @RequestMsg NVARCHAR(100);
BEGIN TRANSACTION;
BEGIN DIALOG @InitDlgHandle
FROM SERVICE [//InstDB/2InstSample/InitiatorService]
TO SERVICE N'//TgtDB/2InstSample/TargetService'
ON CONTRACT [//BothDB/2InstSample/SimpleContract]
WITH
ENCRYPTION = ON;
SELECT @RequestMsg = N'<RequestMsg>Message for Target service.</RequestMsg>';
SEND ON CONVERSATION @InitDlgHandle
MESSAGE TYPE [//BothDB/2InstSample/RequestMessage]
(@RequestMsg);
SELECT @RequestMsg AS SentRequestMsg;
COMMIT TRANSACTION;
GO
--======================================================================================================================
--Step 6
--======================================================================================================================
USE InstInitiatorDB;
GO
DECLARE @RecvReplyMsg NVARCHAR(100);
DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER;
BEGIN TRANSACTION;
WAITFOR
( RECEIVE TOP(1)
@RecvReplyDlgHandle = conversation_handle,
@RecvReplyMsg = message_body
FROM InstInitiatorQueue
), TIMEOUT 1000;
END CONVERSATION @RecvReplyDlgHandle;
-- Display recieved request.
SELECT @RecvReplyMsg AS ReceivedReplyMsg;
COMMIT TRANSACTION;
GO
Copy from msdn, does not work...
编辑器加载中...