Message handler and EF Context

msmq
nservicebus

(Steve Smith) #1

i have a handler class in my project

public class ProcessBatchOrdersHandler : IHandleMessages<ProcessBatchOrdersCommand>
{
    orderImport.ClientExists(message);
}

my Business logic calls my DA layer where …

 using (var context = new IRSDbContextLive())
        {   // Execute the queries and return a result

            var res = context.Clients.Any(x => x.Code.Equals(importRequest.ClientCode, StringComparison.InvariantCultureIgnoreCase));
            return res;

This unfortunately blows up with this stack trace:

at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.b__5()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Linq.Enumerable.Single[TSource](IEnumerable1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.b__3[TResult](IEnumerable1 sequence) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.Any[TSource](IQueryable1 source, Expression1 predicate)
at IForce.IRS.DataAccess.Components.OrderImportV2.ImportV2Dac.ClientExists(Order importRequest) in C:_projects\RG-master\RG-master\DataAccess\Components\OrderImportV2\ImportV2Dac.cs:line 387
at IForce.IRS.BusinessLogic.Components.OrderImportV2.OrderImport.ClientExists(rOrder importRequest) in C:_projects\rg-master\RG-master\BusinessLogic\Components\OrderImportV2\OrderImport.cs:line 56
at InternalWebApi.Handlers.ProcessBatchOrdersHandler.<>c__DisplayClass0_0.b__0() in C:_projects\RG-master\RG-master\InternalWebApi\Handlers\ProcessBatchOrdersHandler.cs:line 31

the nested inner exception says:

The partner transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D025)

I suspect this is to do with the EF context and the Async nature of the handler class. If this is the case is there any work around for me to query my DB via the result of the message i get?


(Ramon Smits) #2

Steve,

You are using MSMQ, by default we will use MSDTC to enable a distributed transaction between the endpoint host and SQL server. For MSDTC to work you will have to allow distributed transactions both on the machines and on the network.

The following might help:

Alternative you can:

Do be aware that this can have an impact on your data consistency and can potentially result in corruption. We also have the outbox which has similar exact-once semantics as when you would use MSDTC.

When you use the Outbox you will not be relying on MSDTC and use regular database transactions but this only works if you access a single database while processing a single message.

Regards,
Ramon