// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using System.Threading;
using GFramework.Cqrs.Abstractions.Cqrs;
using GFramework.Cqrs.Cqrs;
namespace GFramework.Cqrs.Tests.Cqrs;
///
/// 记录缓存 executor 复用场景下每次分发注入到 behavior 的上下文与实例身份。
///
internal sealed class DispatcherPipelineContextRefreshBehavior
: CqrsContextAwareHandlerBase,
IPipelineBehavior
{
private readonly int _instanceId = DispatcherPipelineContextRefreshState.AllocateBehaviorInstanceId();
///
/// 记录当前 behavior 实例实际收到的上下文,然后继续执行下游处理器。
///
/// 当前请求。
/// 下一个处理阶段。
/// 取消令牌。
/// 下游处理结果。
public async ValueTask Handle(
DispatcherPipelineContextRefreshRequest request,
MessageHandlerDelegate next,
CancellationToken cancellationToken)
{
DispatcherPipelineContextRefreshState.RecordBehavior(request.DispatchId, _instanceId, Context);
return await next(request, cancellationToken).ConfigureAwait(false);
}
}