bilpor
bilpor

Reputation: 3889

MSMQ ignoring transaction scope

I'm trying to read data from an MSMQ private Queue and I'm trying to Test a failure so that I can be confident that if the process doesn't finish, then the details stay on the queue. At the moment within my transaction scope it hit's my throw new Exception line and drops into the catch block as expected, but it looks as though the transaction.Complete gets run, because after the exception has been thrown, the queue is empty: here's my code snippet -

try
        {

            using (TransactionScope transaction = new TransactionScope())
            {

                Message incoming = new Message
                {
                    Formatter = formatter,
                    AcknowledgeType = AcknowledgeTypes.FullReceive,
                    Recoverable = true

                };

                incoming = msgQ.Receive(new TimeSpan(0, 0, 3), MessageQueueTransactionType.Single);

                if (incoming != null)
                {
                    MemoryStream mem = (MemoryStream) incoming.BodyStream;
                    mem.Seek(0, SeekOrigin.Begin);
                    IFormatter ifm = new BinaryFormatter();
                    var deserialisedMessage = (TravelMessageServiceObjects) ifm.Deserialize(mem); 
                    ISubmissionsService submissionsService = new SubmissionsService();
                    bool retVal = submissionsService.PerformSubmission(deserialisedMessage.Products, deserialisedMessage.PolicyReference);
                    if (!retVal)
                    {

                        string errorMessage = string.Concat("Policy Ref: ", deserialisedMessage.PolicyReference,
                            " Product: ", Enum.GetName(typeof(Products), deserialisedMessage.Products));
                        throw new Exception(errorMessage);
                    }
                }
                transaction.Complete();
            }

        }
        catch (Exception ex)
        {
            IError logger = new Logger();
            logger.Log(this, SeverityEnum.Warning, ex);

        }

Upvotes: 4

Views: 2134

Answers (1)

Backs
Backs

Reputation: 24903

Create your queue as transactional

enter image description here

And set MessageQueueTransactionType to Automatic. Single just works with internal Message Queuing transactions.

incoming = msgQ.Receive(new TimeSpan(0, 0, 3), MessageQueueTransactionType.Automatic);

Check, MSDTC is working.

Check, firewalls don't block communication

Upvotes: 4

Related Questions