【问题标题】:Masstransit .NET: Is it possible to get the response in consume filter?Masstransit .NET:是否可以在消费过滤器中获得响应?
【发布时间】:2021-10-20 13:17:38
【问题描述】:

目前我们在 Masstransit 中介中使用了一组消费过滤器,即用于验证、交易等。在 API 控制器和消费者之间,我们将使用请求/响应

startup.cs 中的示例:

services.AddMediator(x =>
                {
                    x.ConfigureMediator((context, cfg) =>
                    {
                        cfg.UseConsumeFilter(typeof(LoggingConsumeFilter<>), context);
                        cfg.UseConsumeFilter(typeof(ValidationFilter<>),context);
                        cfg.UseConsumeFilter(typeof(ContextUserFilter<>),context);
                        cfg.UseConsumeFilter(typeof(TransactionConsumeFilter<>),context);
                    });

                    x.AddConsumersFromNamespaceContaining<GetDynFieldSectionModelQueryConsumer>();

现在我计划添加一个新的过滤器“AnonymisationConsumeFilter”,这个想法是过滤器会在消费者完成后改变响应。在我们的例子中,在某些情况下(例如,如果用户无权访问 VIP 用户),响应应该是匿名的。

第一个想法是添加这样的过滤器:

public class AnonymisationConsumeFilter<TMessage> : IFilter<ConsumeContext<TMessage>>
    where TMessage : class
{
    private readonly ILogger<TMessage> logger;
    private readonly IHttpContextAccessor httpContextAccessor;
    private readonly RoleAccessLevelOptions roleAccessLevelOptions;

    public AnonymisationConsumeFilter(ILogger<TMessage> logger, IHttpContextAccessor httpContextAccessor, IOptions<RoleAccessLevelOptions> roleAccessLevelOptions)
    {
        this.logger = logger;
        this.httpContextAccessor = httpContextAccessor;
        this.roleAccessLevelOptions = roleAccessLevelOptions.Value;
    }

    public void Probe(ProbeContext context)
    {
        context.CreateFilterScope("make-anonymisation");
    }

    public async Task Send(ConsumeContext<TMessage> context, IPipe<ConsumeContext<TMessage>> next)
    {
        var requestName = typeof(TMessage).Name;

        await next.Send(context);

        if (context.<how to get the response???> is not IResponseAccessLevel objectWithAccessLevel)
        {
            logger.LogWarning($"----- Make anonymisation for non IResponseAccessLevel objects is ignored, Request {requestName} {context.Message}");
        }
        else
        {
            var response = context.<how to get the response???>
   [...]

             AnonymisationHelper.MakeAnonymous(response);
        }
    }
}

目前我看不到使用消费过滤器从消费者那里获得响应的可能性。我目前看到的唯一可能是使用消息 Payload。但是如何修改过滤器中的响应并返回给调用者呢?

提前谢谢你...

【问题讨论】:

    标签: .net messaging masstransit


    【解决方案1】:

    响应实际上是发送给请求发起者的,因此为了拦截响应,您需要添加一个发送过滤器。范围发送过滤器类似于使用过滤器,但围绕SendContext&lt;T&gt; 构建。范围将在使用和发送过滤器之间共享。

    此外,您可以在将其发送给消费者之前在消费过滤器中的负载中添加一些内容,然后在发送过滤器中检查负载中是否包含ConsumeContext,然后从ConsumeContext 并在发送过滤器中使用它。

    【讨论】:

    • 在 sendFilter 中,我现在可以检查正确的响应类并开始匿名。由于未知原因,发送过滤器被多次调用,用于同一个响应对象。我不知道这是什么原因......目前我设置了一个有效负载来忽略我不需要的额外调用。也许这是由于 Masstransit DI ???
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多