首页 > 解决方案 > MassTransit - 无法覆盖 RoutingSlipResponseProxy 中的 Consume 方法

问题描述

我正在尝试设置一个RoutingSlipResponseProxy,如果没有RequestId. 我试图通过覆盖Consumemy 中的方法来做到这一点RoutingSlipResponseProxy,如下所示:

public class MigrateResponseProxy : RoutingSlipResponseProxy<IMigrationRequested, IMigrationComplete>
{
    public new async Task Consume(ConsumeContext<RoutingSlipCompleted> context)
    {
        var isRequest = context.Message.Variables.ContainsKey("RequestId");
        if (!isRequest)
            return;

        var request = context.Message.GetVariable<IMigrationRequested>("Request");
        var requestId = context.Message.GetVariable<Guid>("RequestId");

        Uri responseAddress = null;
        if (context.Message.Variables.ContainsKey("ResponseAddress"))
            responseAddress = context.Message.GetVariable<Uri>("ResponseAddress");

        if (responseAddress == null)
            throw new ArgumentException($"The response address could not be found for the faulted routing slip: {context.Message.TrackingNumber}");

        var endpoint = await context.GetResponseEndpoint<IMigrationComplete>(responseAddress, requestId).ConfigureAwait(false);

        var response = await CreateResponseMessage(context, request);

        await endpoint.Send(response).ConfigureAwait(false);
    }

    ... remaining code ...
}

这与原始方法的代码基本相同,只是在开头检查了 RequestId。但是,在通过代码进行调试时,似乎永远不会调用这个被覆盖的方法,而是调用父方法。有什么我可能在这里遗漏的吗?任何帮助,将不胜感激。

标签: c#masstransit

解决方案


这些方法不是虚拟的,因此您将无法覆盖它们。在这种情况下,您最好将代理代码复制到您自己的项目中,而不是使用 MassTransit 中包含的实现。

欢迎您提交 PR 以使这些方法虚拟化。


推荐阅读