Example #1
0
    public virtual async Task CompleteAsync(CancellationToken cancellationToken = default)
    {
        // 是否已经进行了回滚操作,如果进行了回滚操作,则不提交工作单元
        if (_isRolledback)
        {
            return;
        }

        // 防止多次调用 Complete 方法,原理就是看 _isCompleting 或者 IsCompleted 是不是已经为 True 了
        PreventMultipleComplete();

        try
        {
            _isCompleting = true;
            await SaveChangesAsync(cancellationToken);

            while (LocalEvents.Any() || DistributedEvents.Any())
            {
                // 发布本地事件
                if (LocalEvents.Any())
                {
                    var localEventsToBePublished = LocalEvents.OrderBy(e => e.EventOrder).ToArray();
                    LocalEvents.Clear();
                    await UnitOfWorkEventPublisher.PublishLocalEventsAsync(localEventsToBePublished);
                }

                // 发布分布式事件
                if (DistributedEvents.Any())
                {
                    var distributedEventsToBePublished = DistributedEvents.OrderBy(e => e.EventOrder).ToArray();
                    DistributedEvents.Clear();
                    await UnitOfWorkEventPublisher.PublishDistributedEventsAsync(distributedEventsToBePublished);
                }

                await SaveChangesAsync(cancellationToken);
            }

            await CommitTransactionsAsync();

            IsCompleted = true;

            // 数据储存了,事务提交了,则说明工作单元已经完成了,遍历完成事件集合,依次调用这些方法
            await OnCompletedAsync();
        }
        catch (Exception ex)
        {
            // 一旦在持久化或者是提交事务时出现了异常,则往上层抛出
            _exception = ex;
            throw;
        }
    }
Example #2
0
        public virtual async Task CompleteAsync(CancellationToken cancellationToken = default)
        {
            if (_isRolledback)
            {
                return;
            }

            PreventMultipleComplete();

            try
            {
                _isCompleting = true;
                await SaveChangesAsync(cancellationToken);

                while (LocalEvents.Any() || DistributedEvents.Any())
                {
                    if (LocalEvents.Any())
                    {
                        var localEventsToBePublished = LocalEvents.OrderBy(e => e.EventOrder).ToArray();
                        LocalEvents.Clear();
                        await UnitOfWorkEventPublisher.PublishLocalEventsAsync(
                            localEventsToBePublished
                            );
                    }

                    if (DistributedEvents.Any())
                    {
                        var distributedEventsToBePublished = DistributedEvents.OrderBy(e => e.EventOrder).ToArray();
                        DistributedEvents.Clear();
                        await UnitOfWorkEventPublisher.PublishDistributedEventsAsync(
                            distributedEventsToBePublished
                            );
                    }

                    await SaveChangesAsync(cancellationToken);
                }

                await CommitTransactionsAsync();

                IsCompleted = true;
                await OnCompletedAsync();
            }
            catch (Exception ex)
            {
                _exception = ex;
                throw;
            }
        }