Dorana
Dorana

Reputation: 331

Problems with transactionScope and ServiceBus in production environment

I have seen other with similar issues but I have yet to find a working solution.

I'm using a transaction while calling Database and ServiceBus.

I'm using a UnitOfWork/EntityFramework for my database.

Here is my code

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

And here is a snippet from the ExportGroups function

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

}

This works without any issues in my development environment. But when we put it into production I get the following error

"System.InvalidOperationException: Local transactions are not supported with other resource managers/DTC."

The DTC is enabled on the machine and we use it to process other transactions just fine, though none of them use the ServiceBus in them.

[EDIT = Added stack trace]

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)

Upvotes: 4

Views: 6514

Answers (2)

Dmytro Tysko
Dmytro Tysko

Reputation: 73

using (TransactionScope scope = new TransactionScope()
{
    //Database call

    using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled))
    {
        sender.Send(message);
        transaction.Complete();
    }
}

Upvotes: 2

Sreeram Garlapati
Sreeram Garlapati

Reputation: 4993

Transactions are not supported across different Windows Azure Services. That said there are many ways to send messages to service bus with transactional guarantees.

HTH! Sree

Upvotes: 0

Related Questions