如何通过 RabbitMQ 请求传递 headers

How to pass headers with RabbitMQ request

我有以下代码创建和 IRequestClient 客户端并发送请求并等待响应。在这里你可以看到,我已经设置了一些 headers 值以及 GreenPipes

public async Task<CustomerData> GetCustomerData(CustomerDetailsReq request, CHeader headerKey)
{
    CustomerData rtnRes = new();

    using (var req = _clientCustomerDetailsReq.Create(request))
    {
        req.UseExecute(x => x.Headers.Set("datetime", headerKey.datetime));
        req.UseExecute(x => x.Headers.Set("languageCode", headerKey.languageCode));
        req.UseExecute(x => x.Headers.Set("version", headerKey.version));

        var response = await req.GetResponse<CustomerData>();
        rtnRes = response.Message;
    }

    return rtnRes;
}

现在,我需要我的要求是,我不需要来自消费者端的任何响应,只是想将请求传递给具有一些 header 值的消费者。为此,我只是发布命令并忘记它。我的代码如下,

IPublishEndpoint _publishEndpoint;

public CustomerService(IPublishEndpoint publishEndpoint)
{
    _publishEndpoint = publishEndpoint;
}

public async Task<string> TestMethod(CustomerDetailsReq request)
{
    string datas = "";
    try
    {
        await _publishEndpoint.Publish<CustomerDetailsReq>(orderRequest);
    }
    catch (Exception)
    {
        throw;
    }
    return datas;
}

但是现在,我担心的是,我该如何传递 header 值?

您可以指定 headers 与 Publish 调用内联:

await _publishEndpoint.Publish<CustomerDetailsReq>(orderRequest, context =>
{
    context.Headers.Set("datetime", headerKey.datetime);
    context.Headers.Set("languageCode", headerKey.languageCode);
    context.Headers.Set("version", headerKey.version);
});