Gamma
Gamma

Reputation: 347

How to pass headers with RabbitMQ request

I have following code which create and IRequestClient client and send request and waiting for the response. here you can see, I have set some headers value as well with 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;
}

Now, I need my requirement is, I don't need any response from Consumer side, and just wanted to pass the request to consumer with some header value. To achieve that, I just publish the command and forget it. my code as follows,

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;
}

But now, my concern is, how can I pass the header values with this?

Upvotes: 0

Views: 1237

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33258

You can specify the headers inline with the Publish call:

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

Upvotes: 2

Related Questions