c# - MSMQ 忽略事务范围

标签 c# msmq transactionscope

我正在尝试从 MSMQ 私有(private)队列中读取数据,并且正在尝试测试失败,以便我可以确信如果进程没有完成,那么详细信息将保留在队列中。目前在我的事务范围内,它命中了我的 throw new Exception 行并按预期落入 catch block ,但看起来 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);

        }

最佳答案

创建事务队列

enter image description here

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

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

检查,MSDTC正在工作。

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

关于c# - MSMQ 忽略事务范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31960597/

相关文章:

c# - 通过散列/加密验证某些东西是 "done"

c# - MSMQ 和 Active Directory 集成

.net - Entity Framework 和事务范围在处理事务范围后不会恢复隔离级别

c# - 带有 MySQL 和分布式事务的 TransactionScope

c# - 具有嵌套 sql 事务的 TransactionScope

c# - 使用 Type 变量调用正确的泛型方法,带 out 和 ref

c# - 嵌套和非嵌套 else/else ifs 之间有区别吗?

c# - 如何在当前项目中已存在的 T4 文本模板中引用类?

c# - 消息队列 - 同时处理多个响应 - C#.NET

ajax - ASP.Net Web API 中的消息队列