代码之家  ›  专栏  ›  技术社区  ›  bilpor

MSMQ忽略事务作用域

  •  3
  • bilpor  · 技术社区  · 9 年前

    我正在尝试从MSMQ专用队列读取数据,并尝试测试一个失败,这样我就可以确信,如果进程没有完成,那么详细信息将保留在队列中。目前,在我的事务范围内,它命中了我抛出的新异常行,并按预期落入捕获块,但看起来好像是事务。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 回复  |  直到 9 年前
        1
  •  4
  •   Backs    9 年前

    将队列创建为事务性队列

    enter image description here

    并设置 MessageQueueTransactionType Automatic . Single 仅适用于内部消息队列事务。

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

    检查 MSDTC 正在工作。

    检查,防火墙不会阻止通信