【问题标题】:Problems with transactionScope and ServiceBus in production environment生产环境中transactionScope和ServiceBus的问题
【发布时间】:2014-10-10 11:23:45
【问题描述】:

我见过其他有类似问题的人,但我还没有找到可行的解决方案。

我在调用 Database 和 ServiceBus 时正在使用事务。

我正在为我的数据库使用 UnitOfWork/EntityFramework。

这是我的代码

/*Creating the list and adding to UnitOfWork repository*/
....
using (TransactionScope scope = new TransactionScope())
        {
            _unitOfWork.Save();
            ExportGroups(myGroupList);
            scope.Complete();
        }

这是来自 ExportGroups 函数的 sn-p

public void ExportGroups(IEnumerable<Group> groups)
{
     /*Generating BrokeredMessage message*/
     Task.Factory.StartNew(() =>
        {
            MessagingFactory factory = CreateMessagingFactory(...);
            if (factory != null)
            {
                var sender = factory.CreateMessageSender(topicName);
                sender.Send(message);
            }
        }).Wait();

}

这在我的开发环境中没有任何问题。但是当我们将它投入生产时,我得到了以下错误

“System.InvalidOperationException:其他资源管理器/DTC 不支持本地事务。”

机器上启用了 DTC,我们用它来处理其他事务就好了,尽管它们都没有使用其中的 ServiceBus。

[编辑 = 添加堆栈跟踪]

Server stack trace: 
   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpResourceManager.EnlistAsyncResult..ctor(SbmpResourceManager resourceManager, Transaction transaction, IRequestSessionChannel channel, SbmpMessageCreator messageCreator, Action`1 partitionInfoSetter, TimeSpan timeout, AsyncCallback callback, Object state)
   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpResourceManager.BeginEnlist(Transaction transaction, IRequestSessionChannel channel, SbmpMessageCreator messageCreator, Action`1 partitionInfoSetter, TimeSpan timeout, AsyncCallback callback, Object state)
   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpTransactionalAsyncResult`1.<>c__DisplayClass3e.<GetAsyncSteps>b__38(TIteratorAsyncResult thisPtr, TimeSpan t, AsyncCallback c, Object s)
   at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.EnumerateSteps(CurrentThreadType state)
   at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.Start()

Exception rethrown at [0]: 
   at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageSender.EndSendCommand(IAsyncResult result)
   at Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageSender.OnEndSend(IAsyncResult result)
   at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.EnumerateSteps(CurrentThreadType state)

Exception rethrown at [1]: 
   at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.RunSynchronously()
   at Microsoft.ServiceBus.Messaging.MessageSender.Send(TrackingContext trackingContext, IEnumerable`1 messages, TimeSpan timeout)
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at ....GroupService.ExportGroups(IEnumerable<Group> groups)

【问题讨论】:

  • 据我所知can't achieve this(请参阅更新下的部分答案)。服务总线doesn't support DTC。如果你在Windows Azure,你可以使用this approach
  • 谢谢迈克尔。我在寻找答案时注意到了这两个线程。令我困惑的是,它在调试模式下从 Visual Studio 运行系统时可以正常工作。这就是让我相信这确实是可能的。
  • 附注。经过一些本地故障跟踪和测试之后,似乎在 Visual Studio 上运行它时甚至没有使用 DTC,这对我来说更加令人费解。我在我的开发环境中停止了 DTC 服务,但该功能仍然有效。适用于更新两个实体,如果 ExportGroups 调用抛出异常,它会处理数据库的回滚
  • 这件事的另一个奇怪的发展。我添加它以使用 NInject 块(基于同事的提示)一旦部署,我设法让它工作一次,它保存到数据库并将消息推送到服务总线。当重试相同的过程时,我们又回到了上述错误。

标签: c# entity-framework transactionscope unit-of-work servicebus


【解决方案1】:

不支持跨不同 Windows Azure 服务的事务。也就是说有很多ways to send messages to service bus with transactional guarantees

HTH! 斯里

【讨论】:

    【解决方案2】:
    using (TransactionScope scope = new TransactionScope()
    {
        //Database call
    
        using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled))
        {
            sender.Send(message);
            transaction.Complete();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      相关资源
      最近更新 更多