BEGIN DIALOG CONVERSATION (Transact-SQL)
Begins a dialog from one service to another service. A dialog is a conversation that provides exactly-once-in-order messaging between two services.
BEGIN DIALOG [ CONVERSATION ] @dialog_handle
FROM SERVICE initiator_service_name
TO SERVICE 'target_service_name'
[ , { 'service_broker_guid' | 'CURRENT DATABASE' }]
[ ON CONTRACT contract_name ]
[ WITH
[ { RELATED_CONVERSATION = related_conversation_handle
| RELATED_CONVERSATION_GROUP = related_conversation_group_id } ]
[ [ , ] LIFETIME = dialog_lifetime ]
[ [ , ] ENCRYPTION = { ON | OFF } ] ]
[ ; ]
All messages are part of a conversation. Therefore, an initiating service must begin a conversation with the target service before sending a message to the target service. The information specified in the BEGIN DIALOG CONVERSATION statement is similar to the address on a letter; Service Broker uses the information to deliver messages to the correct service. The service specified in the TO SERVICE clause is the address that messages are sent to. The service specified in the FROM SERVICE clause is the return address used for reply messages.
The target of a conversation does not need to call BEGIN DIALOG CONVERSATION. Service Broker creates a conversation in the target database when the first message in the conversation arrives from the initiator.
Beginning a dialog creates a conversation endpoint in the database for the initiating service, but does not create a network connection to the instance that hosts the target service. Service Broker does not establish communication with the target of the dialog until the first message is sent.
When the BEGIN DIALOG CONVERSATION statement does not specify a related conversation or a related conversation group, Service Broker creates a new conversation group for the new conversation.
Service Broker does not allow arbitrary groupings of conversations. All conversations in a conversation group must have the service specified in the FROM clause as either the initiator or the target of the conversation.
The BEGIN DIALOG CONVERSATION command locks the conversation group that contains the dialog_handle returned. When the command includes a RELATED_CONVERSATION_GROUP clause, the conversation group for dialog_handle is the conversation group specified in the related_conversation_group_id parameter. When the command includes a RELATED_CONVERSATION clause, the conversation group for dialog_handle is the conversation group associated with the related_conversation_handle specified.
BEGIN DIALOG CONVERSATION is not valid in a user-defined function.
A. Beginning a dialog
The following example begins a dialog conversation and stores an identifier for the dialog in @dialog_handle. The //Adventure-Works.com/ExpenseClient service is the initiator for the dialog, and the //Adventure-Works.com/Expenses service is the target of the dialog. The dialog follows the contract //Adventure-Works.com/Expenses/ExpenseSubmission.
DECLARE @dialog_handle UNIQUEIDENTIFIER ; BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/ExpenseClient] TO SERVICE '//Adventure-Works.com/Expenses' ON CONTRACT [//Adventure-Works.com/Expenses/ExpenseSubmission] ;
B. Beginning a dialog with an explicit lifetime
The following example begins a dialog conversation and stores an identifier for the dialog in @dialog_handle. The //Adventure-Works.com/ExpenseClient service is the initiator for the dialog, and the //Adventure-Works.com/Expenses service is the target of the dialog. The dialog follows the contract //Adventure-Works.com/Expenses/ExpenseSubmission. If the dialog has not been closed by the END CONVERSATION command within 60 seconds, the broker ends the dialog with an error.
DECLARE @dialog_handle UNIQUEIDENTIFIER ; BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/ExpenseClient] TO SERVICE '//Adventure-Works.com/Expenses' ON CONTRACT [//Adventure-Works.com/Expenses/ExpenseSubmission] WITH LIFETIME = 60 ;
C. Beginning a dialog with a specific broker instance
The following example begins a dialog conversation and stores an identifier for the dialog in @dialog_handle. The //Adventure-Works.com/ExpenseClient service is the initiator for the dialog, and the //Adventure-Works.com/Expenses service is the target of the dialog. The dialog follows the contract //Adventure-Works.com/Expenses/ExpenseSubmission. The broker routes messages on this dialog to the broker identified by the GUID a326e034-d4cf-4e8b-8d98-4d7e1926c904.
DECLARE @dialog_handle UNIQUEIDENTIFIER ;
BEGIN DIALOG CONVERSATION @dialog_handle
FROM SERVICE [//Adventure-Works.com/ExpenseClient]
TO SERVICE '//Adventure-Works.com/Expenses',
'a326e034-d4cf-4e8b-8d98-4d7e1926c904'
ON CONTRACT [//Adventure-Works.com/Expenses/ExpenseSubmission] ;
D. Beginning a dialog, and relating it to an existing conversation group
The following example begins a dialog conversation and stores an identifier for the dialog in @dialog_handle. The //Adventure-Works.com/ExpenseClient service is the initiator for the dialog, and the //Adventure-Works.com/Expenses service is the target of the dialog. The dialog follows the contract //Adventure-Works.com/Expenses/ExpenseSubmission. The broker associates the dialog with the conversation group identified by @conversation_group_id instead of creating a new conversation group.
DECLARE @dialog_handle UNIQUEIDENTIFIER ; DECLARE @conversation_group_id UNIQUEIDENTIFIER ; SET @conversation_group_id = <retrieve conversation group ID from database> BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/ExpenseClient] TO SERVICE '//Adventure-Works.com/Expenses' ON CONTRACT [//Adventure-Works.com/Expenses/ExpenseSubmission] WITH RELATED_CONVERSATION_GROUP = @conversation_group_id ;
E. Beginning a dialog with an explicit lifetime, and relating the dialog to an existing conversation
The following example begins a dialog conversation and stores an identifier for the dialog in @dialog_handle. The //Adventure-Works.com/ExpenseClient service is the initiator for the dialog, and the //Adventure-Works.com/Expenses service is the target of the dialog. The dialog follows the contract //Adventure-Works.com/Expenses/ExpenseSubmission. The new dialog belongs to the same conversation group that @existing_conversation_handle belongs to. If the dialog has not been closed by the END CONVERSATION command within 600 seconds, Service Broker ends the dialog with an error.
DECLARE @dialog_handle UNIQUEIDENTIFIER DECLARE @existing_conversation_handle UNIQUEIDENTIFIER SET @existing_conversation_handle = <retrieve conversation handle from database> BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/ExpenseClient] TO SERVICE '//Adventure-Works.com/Expenses' ON CONTRACT [//Adventure-Works.com/Expenses/ExpenseSubmission] WITH RELATED_CONVERSATION = @existing_conversation_handle LIFETIME = 600 ;
F. Beginning a dialog with optional encryption
The following example begins a dialog and stores an identifier for the dialog in @dialog_handle. The //Adventure-Works.com/ExpenseClient service is the initiator for the dialog, and the //Adventure-Works.com/Expenses service is the target of the dialog. The dialog follows the contract //Adventure-Works.com/Expenses/ExpenseSubmission. The conversation in this example allows the message to travel over the network without encryption if encryption is not available.
DECLARE @dialog_handle UNIQUEIDENTIFIER BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/ExpenseClient] TO SERVICE '//Adventure-Works.com/Expenses' ON CONTRACT [//Adventure-Works.com/Expenses/ExpenseSubmission] WITH ENCRYPTION = OFF ;