Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading