title: How to do a transactional GET from MQ using the .net TransactionScope?
body: <p>I have a .NET TransactionScope that needs to contain both a MSSQL database and a IBM MQ queue.</p>
  
  <p>I'm using .NET 4.0 (VS2010), SQL 2008R2, MQ Server 6.0, MQ Client 7.0.1.9 in fully managed mode. All components are running on different machines.</p>
  
  <p>From what I've found the following pattern should work:
  <a href="http://publib.boulder.ibm.com/infocenter/wmqv7/v7r1/index.jsp?topic=%2Fcom.ibm.mq.doc%2Fun11400_.htm" rel="nofollow">http://publib.boulder.ibm.com/infocenter/wmqv7/v7r1/index.jsp?topic=%2Fcom.ibm.mq.doc%2Fun11400_.htm</a></p>
  
  <blockquote>
    <p>Implicit Transaction
        The following piece of code describes how a WebSphere MQ .NET application puts a message using .NET implicit transaction
    programming. </p>
    
    <p>Using (TransactionScope scope = new TransactionScope ()) {    Q.Put
    (putMsg,pmo);     scope.Complete (); }</p>
    
    <p>Q.close(); qMgr.Disconect();}</p>
  </blockquote>
  
  <p>In my code this looks like:</p>
  
  <pre><code>  // mq properties
    properties = new Hashtable();
    properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
    properties.Add(MQC.HOST_NAME_PROPERTY, HOSTNAME);
    properties.Add(MQC.PORT_PROPERTY, PORTNUMBER);
    properties.Add(MQC.CHANNEL_PROPERTY, CHANNELNAME);
  
    _queueManager = new MQQueueManager(queueManagerName, properties);
    _queue = _queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
    _gmo = new MQGetMessageOptions();
    _gmo.Options |= MQC.MQGMO_WAIT;
    _gmo.Options |= MQC.MQGMO_SYNCPOINT;
    _gmo.WaitInterval = 1000;  // 1 seconds wait
  
    // in a loop
    using (TransactionScope t = new TransactionScope())
    {
  
    var message = new MQMessage();
    try
    {
       _queue.Get(message, _gmo);
    }
    catch (MQException mqe)
    { 
       message = null;
       if (mqe.ReasonCode == 2033)
       {
            Console.WriteLine("No message available");
       }
       else
       {
            throw;
        }
     }
     t.Complete();
   }
  
  //Afterwards:
  
    if (_queue != null)
     {
    _queue.Close();
    _queue = null;
    }
    if (_queueManager != null)
    {
    _queueManager.Disconnect();
    _queueManager.Close();
    _queueManager = null;
    }
  </code></pre>
  
  <p>The problem with this is that all messages re-appear in the queue after the application is closed, while the work in the SQL database is committed.
  If an exception happens inside the transactionscope, the SQL transaction rolls back while the messages in MQ appear to stay removed (until I restart the application).
  Also, I don't see any DTC activity between the .NET client and the MQ server (is that expected?)</p>
  
  <p>I'm kind of lost here, any help is greatly appreciated.</p>
  
  <ul>
  <li>Which MQ client should I use? Managed, Unmanaged, XA ? </li>
  <li>Should I see DTC activity? How would I get the managed client to participate?</li>
  <li>Which objects should be created inside the transactionscope?</li>
  <li>Why do messages re-appear on the queue when the application ends even when I commit the transactionscope?</li>
  </ul>
  
  <p><strong>UPDATE</strong></p>
  
  <ul>
  <li>I've had this working with 7.1 on both client and server, but to get that (server) version deployed on our production environment would take too long. </li>
  <li>So I need to work with version 7.0.x on the server side, clientside I can use whatever I need.</li>
  <li>Connecting to a MQ6 server using the 7.1 client fails with errorcode 2354</li>
  </ul>