Custom Header with IMessageSession

I have a fairly simple SendOnly API endpoint that I wish to include a custom header when sending the message.

Building from the samples, I have a class that has IMessageSession injected into it, and from there calling publish passing some send options. (nothing fancy was done in endpoint configuration for the IMessageSession to be injected, its just worked)

My issue is that the custom header doesnt seem to be added to the message. First question I suppose, does IMessageSession support custom headers in this manner. I’ve seen some discussion about some header and transactional things that it doesnt support. But the method supports the syntax so I’m thinking it should work??

Second, if I need to use IMessageHandlerContext instead, how can I get that to do dependency injected? Right now I’m using AspCore 3.1 with the MS default container. NSB 7.7.0


sample code

public SimpleApi(IMessageSession messageSession)
    _messageSession = messageSession;        

public async Task PublishEvent(object message)
    var sendOptions = new PublishOptions();
    sendOptions.SetHeader("MyCustomHeader", "MyCustomValue");

    await _messageSession.Publish(message, sendOptions);

Hi @StephenB,

the SetHeader method should work the way you’ve presented it in the snippet. How are you checking if the header is set or not?

The IMessageHandlerContext is designed to be used inside a message handler which is not what you are doing i.e. sending messages from a web API controller.


Well as usual user error. Since you said it should work, I went back and try commands instead of events etc… turns out that on my handler side I’m getting the event and converting into a command and resending. I was trying to pull the header from that last command handler, but forget to extract it from the event handler and pass it forward, duh.

Thanks for confirming it should work and setting on the right path.