MSMQ忽略了事务范围

时间:2015-08-12 09:03:29

标签: c# msmq transactionscope

我正在尝试从MSMQ专用队列中读取数据而我正在尝试测试失败,以便我可以确信如果进程没有完成,那么详细信息将保留在队列中。在我的事务范围内的那一刻,它命中了我的抛出新的Exception行并按预期进入catch块,但看起来好像运行了transaction.Complete,因为在抛出异常之后,队列是空的:这是我的代码段 -

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);

        }

1 个答案:

答案 0 :(得分:4)

将您的队列创建为事务性

enter image description here

并将MessageQueueTransactionType设为AutomaticSingle只适用于内部消息队列事务。

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

检查,MSDTC正在运作。

检查,防火墙不阻止通信