Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using SQL-Server then you should use a distributed transaction, since both MSMQ and SQL-Server support it. What happens is you wrap your database write in a TransactionScope block and call scope.Complete() only if it succeeds. If it fails, then when your WCF method returns the message will be placed back into the queue to be tried again. Here's a trimmed version of code I use:</p> <pre><code> [OperationBehavior(TransactionScopeRequired=true, TransactionAutoComplete=true)] public void InsertRecord(RecordType record) { try { using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { SqlConnection InsertConnection = new SqlConnection(ConnectionString); InsertConnection.Open(); // Insert statements go here InsertConnection.Close(); // Vote to commit the transaction if there were no failures scope.Complete(); } } catch (Exception ex) { logger.WarnException(string.Format("Distributed transaction failure for {0}", Transaction.Current.TransactionInformation.DistributedIdentifier.ToString()), ex); } } </code></pre> <p>I test this by queueing up a large but known number of records, let WCF start lots of threads to handle many of them simultaneously (reaches 16 threads--16 messages off the queue at once), then kill the process in the middle of operations. When the program is restarted the messages are read back from the queue and processed again as if nothing happened, and at the conclusion of the test the database is consistent and has no missing records.</p> <p>The Distributed Transaction Manager has an ambient presence, and when you create a new instance of TransactionScope it automatically searches for the current transaction within the scope of the method invokation--which should have been created already by WCF when it popped the message off the queue and invoked your method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload