yang-xiaodong / eShopOnContainers

Replace eShopOnContainers EventBus with CAP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Upgrading to 2.6.0 breaks OrderingIntegrationEventService

thisispaulsmith opened this issue · comments

ICapPublisher has change to use AsyncLocal when getting the ICapTransation. Accessing this by value returns null.

What's the recommended approach for resolving?

_eventBus.Transaction.Begin(_orderingContext.GetCurrentTransaction().GetDbTransaction());

becomes

_eventBus.Transaction.Value.Begin(_orderingContext.GetCurrentTransaction().GetDbTransaction());

but throws NullReferenceException

You need set _eventBus.Transaction at first, see below codes :

public static IDbTransaction BeginTransaction(this IDbConnection dbConnection,
    ICapPublisher publisher, bool autoCommit = false)
{
    if (dbConnection.State == ConnectionState.Closed)
    {
        dbConnection.Open();
    }

    var dbTransaction = dbConnection.BeginTransaction();
    
    // Note: need to set value first at here  
    publisher.Transaction.Value = publisher.ServiceProvider.GetService<CapTransactionBase>();
    
    // Note: then set dbTransaction to CAP transaction
    var capTransaction = publisher.Transaction.Value.Begin(dbTransaction, autoCommit);
    
    return (IDbTransaction)capTransaction.DbTransaction;
}