值不能为空。参数名称:dbContextTransaction-IntegrationEvent c#

时间:2019-05-09 12:06:39

标签: c# asp.net entity-framework microservices event-bus

我正在使用EventBus通知其他微服务。保存客户时。事件集成必须使其他微服务保持同步 我正在进行事件整合。我从Microsoft获得了有关事件总线的示例。

应用程序以这种方式到达时

public async Task AddAndSaveEventAsync(IntegrationEvent evt)
{
     await _eventLogService.SaveEventAsync(evt, _osDataContext.GetCurrentTransaction.GetDbTransaction());
}

通过此方法输入后不久,它将通过另一种发生错误的方法

namespace Microsoft.EntityFrameworkCore.Storage
{
    public static class DbContextTransactionExtensions
    {
        public static DbTransaction GetDbTransaction([NotNullAttribute] this IDbContextTransaction dbContextTransaction);
    }
}

它返回一个错误:Value can not be null. Parameter name: dbContextTransaction

我有我的数据库上下文类

public partial class ApplicationDataContext : DbContext
{
    public ApplicationDataContext(DbContextOptions<ApplicationDataContext> options) : base(options)
    {
    }

    private IDbContextTransaction _currentTransaction;
    public IDbContextTransaction GetCurrentTransaction => _currentTransaction;

    public async Task BeginTransactionAsync()
    {
        _currentTransaction = _currentTransaction ?? await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted);
    }

    public async Task CommitTransactionAsync()
    {
        try
        {
            await SaveChangesAsync();
            _currentTransaction?.Commit();
        }
        catch
        {
            RollbackTransaction();
            throw;
        }
        finally
        {
            if (_currentTransaction != null)
            {
                _currentTransaction.Dispose();
                _currentTransaction = null;
            }
        }
    }

    public void RollbackTransaction()
    {
        try
        {
            _currentTransaction?.Rollback();
        }
        finally
        {
            if (_currentTransaction != null)
            {
                _currentTransaction.Dispose();
                _currentTransaction = null;
            }
        }
    }

    public const string DEFAULT_SCHEMA = "serviceorder";

    public DbSet<model.ServiceOrder> ServiceOrder { get; set; }
    public DbSet<ServiceOrderType> ServiceOrderType { get; set; }
    public DbSet<ContactRole> ContactRole { get; set; }
    public DbSet<Contact> Contact { get; set; }
    public DbSet<DocumentType> DocumentType { get; set; }
    public DbSet<Document> Document { get; set; }
    public DbSet<Customer> Customer { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new ServiceOrderTypeConfiguration());
        modelBuilder.ApplyConfiguration(new ServiceOrderConfiguration());
        modelBuilder.ApplyConfiguration(new CustomerConfiguration());
        modelBuilder.ApplyConfiguration(new DocumentConfiguration());
        modelBuilder.ApplyConfiguration(new DocumentTypeConfiguration());
        modelBuilder.ApplyConfiguration(new ContactConfiguration());
        modelBuilder.ApplyConfiguration(new ContactRoleConfiguration());
    }
}

1 个答案:

答案 0 :(得分:0)

基本上是这个错误。对于不打开连接,就是这样。我正在拨打电话,但没有打开连接。

    public async Task AddAndSaveEventAsync(IntegrationEvent evt)
        {
            await ResilientTransaction.New(_osDataContext).ExecuteAsync(async () =>
               {
                   await _osDataContext.SaveChangesAsync();
                   await _eventLogService.SaveEventAsync(evt, _osDataContext.Database.CurrentTransaction.GetDbTransaction());
               });
        }