Lesson 4: Beginning the Conversation
New: 15 September 2007
In this lesson, you will learn to start a conversation that spans two instances of the Database Engine and send a request message from the initiator instance to the target instance. Run these steps from a copy of Management Studio running on the same computer as the initiator instance.
Switch to the InitiatorDB database
Copy and paste the following code into a Query Editor window. Then, run it to switch context to the InstInitiatorDB database where you will initiate the conversation.
USE InstInitiatorDB; GO
Start a conversation and send a request message
Copy and paste the following code into a Query Editor window. Then, run it to begin a conversation and send a request message to the //TgtDB/2InstSample/TargetService in the InstTargetDB. The code must be run in one block because a variable is used to pass a dialog handle from BEGIN DIALOG to the SEND statement. The batch runs the BEGIN DIALOG statement to begin the conversation, and then builds a request message. Then, it uses the dialog handle in a SEND statement to send the request message on that conversation. The last SELECT statement just displays the text of the message that was sent.
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
You have successfully started a conversation and sent the request message to the target service. Next, you will receive the request message from the target queue and send a reply message to the initiator service. See Lesson 5: Receiving a Request and Sending a Reply.