
USE SodaDB
GO


DECLARE @h UNIQUEIDENTIFIER;
DECLARE @invreq XML;
SET @invreq = '
<InventoryReq xmlns="http://www.company.com/inventoryRequest">
  <order>1</order>
  <product>1040</product>
  <units>3</units>
  <status>Req</status>
</InventoryReq>
';

BEGIN TRY
BEGIN TRANSACTION;
BEGIN DIALOG CONVERSATION @h
 FROM SERVICE InvInitiator_L8A
 TO SERVICE 'InvService_L8A'
 ON CONTRACT InvContract_L8A
 WITH ENCRYPTION = OFF;

-- request for product 1040
SEND ON CONVERSATION @h 
 MESSAGE TYPE InvRequestMsg_L8A (@invreq);
COMMIT;
END TRY
BEGIN CATCH
	PRINT ERROR_MESSAGE()
    ROLLBACK;
END CATCH
GO


--
-- Step 2: Receive message and return the reply
-- This happens at the target. Just send <OK/> this time.
--

DECLARE @h UNIQUEIDENTIFIER;
DECLARE @invreq XML;
DECLARE @invresp XML

BEGIN TRANSACTION;
RECEIVE TOP(1)
 @invreq = CAST(message_body AS XML),
 @h = conversation_handle
 FROM InvRequestQueue_L8A;
PRINT CONVERT(VARCHAR(4000), @invreq);
SEND ON CONVERSATION @h
 MESSAGE TYPE InvResponseMsg_L8A ('
<InventoryReq xmlns="http://www.company.com/inventoryRequest">
  <order>1</order>
  <product>1040</product>
  <units>3</units>
  <status>OK</status>
</InventoryReq>');
END CONVERSATION @h
COMMIT;
GO

--
-- Step 3: Receive the response and end the conversation
-- This happens at the initiator.
--

DECLARE @h UNIQUEIDENTIFIER;
DECLARE @resp XML;

BEGIN TRANSACTION;
RECEIVE TOP(1)
 @resp = CAST(message_body AS XML),
 @h = conversation_handle
 FROM InvResponseQueue_L8A;
PRINT CONVERT(VARCHAR(4000),@resp);
-- You don't need to read the end conversation message
-- You can decide to unconditionally end the conversation
END CONVERSATION @h
COMMIT;
GO

