Visual Basic Code Example: Sending a Message Using an MS DTC External Transaction
Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista
To send a message within an MS DTC external transaction, an MSMQCoordinatedTransactionDispenser object is used to create the transaction object. Once the transaction object is created, it is referenced in the call to MSMQMessage.Send.
Note
Each Message Queuing message can have no more than 4 MB of data.
To send a message using an external transaction
Declare an MSMQCoordinatedTransactionDispenser object.
Call MSMQCoordinatedTransactionDispenser.BeginTransaction to start the transaction.
Open the queue with send access.
Set the needed message properties and call MSMQMessage.Send. There is no limit to the number of messages that can be sent within the transaction.
Code Example
The following example sends a single message within an external transaction.
Dim xdisper as New MSMQCoordinatedTransactionDispenser
Dim xact as MSMQTransaction
Dim qSend as MSMQQueue
Dim msg as New MSMQMessage
Set xact = xdisper.BeginTransaction
' Assume that the queue already exists and is transactional.
Set qSend = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
msg.Label = "MyTransactional message"
msg.Body = "Message 1 Body"
msg.Send qSend, xact 'Associates send with xact.
xact.commit