Compartilhar via


Visual Basic Code Example: Sending a Message Using an Internal 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 internal transaction, the MSMQTransactionDispenser is used to create the transaction object. After the transaction object is created, make sure 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 internal transaction

  1. Declare the objects needed to send a message. This example uses MSMQQueueInfo, MSMQQueue, MSMQMessage, MSMQTransactionDispenser, and MSMQTransaction objects. In this example, MSMQTransactionDispenser object is used because the message is sent in an internal transaction.

  2. Call MSMQQueueInfo.Open to open the transactional queue with send access. This example also calls MSMQQueueInfo.Create to create a transactional queue on the local computer if the specified queue does not exist.

  3. Call MSMQTransactionDispenser.BeginTransaction to start the transaction. From this point on, any errors will cause the function to end prematurely and the transaction will be aborted.

  4. Call MSMQMessage.Send to send the message. In this example the Transaction parameter is set to the transaction object.

  5. Call MSMQTransaction.Commit to commit the transaction.

  6. Call MSMQQueue.Close to close the queue.

Code Example

This example sends a message within an internal transaction. It uses the path name of the queue to specify the destination queue, opens the queue with send access, starts the transaction, sends the message to the queue within the transaction, commits the transaction, and then closes the queue.

To try this example in Microsoft® Visual Basic® (version 5.0 or 6.0), paste the code into the Declaration section of a form, press F5, and click the form.

Option Explicit  
  
' Declare the required objects.  
Dim qinfo As New MSMQQueueInfo  
Dim qSend As MSMQQueue  
Dim msg As New MSMQMessage  
Dim xdisper As New MSMQTransactionDispenser    ' Used for internal transactions  
Dim xact As MSMQTransaction  
  
Private Sub SendInternalXact()  
  
  ' Open the transactional queue with SEND access. The queue is  
  ' created if no such queue exists.  
  qinfo.PathName = ".\TransactionTestQueue"  
  qinfo.Label = "Send Message Test"  
  On Error Resume Next          'Ignore if queue already exists.  
  qinfo.Create IsTransactional:=True  
  On Error GoTo ErrorHandler  
  Set qSend = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)  
  
  ' Start the internal transaction. From this point on, any errors   
  ' will exit this function prematurely and the transaction will   
  ' be aborted. Commit occurs only if explicitly invoked.  
  Set xact = xdisper.BeginTransaction  
  
  ' Send the message to the queue as an internal transaction.  
  msg.Label = "Transactional test message"  
  msg.Body = "Message sent as internal transaction."  
  msg.Send qSend, xact  
  xact.Commit  
  
  ' Close the queue.  
  qSend.Close  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Unexpected error!" + Chr(13) + _  
          "Reason: " + Err.Description _  
         + Chr(13) + "Error: " + Hex(Err.Number)  
  If Not qSend Is Nothing And qSend.IsOpen2 Then   
    qSend.Close  
  End If  
End Sub