Edit

Share via


Lesson 4: Begin the conversation

Applies to: SQL Server Azure SQL Managed Instance

In this lesson, you 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.

Procedures

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 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 AS UNIQUEIDENTIFIER;
DECLARE @RequestMsg AS 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