Reputation: 67
I'm struggling to get message headers to work from my unit tests in nServiceBus (v3.3.0.0). The headers get set in the test using SetIncomingHeader(...), but when I call GetHeader(...) in the message handler, I get the string:
No header get header action was defined, please spicify one using ExtensionMethods.GetHeaderAction = ...
The headers work fine outside of the testing framework.
The FullDuplex sample app basically shows the code I'm using and suffers the same problem. Does anyone know how I set up the GetHeaderAction method?
Thanks in advance.
Upvotes: 2
Views: 738
Reputation: 2618
As mentioned by John, this is fixed in
Also, make sure that you are using the NServiceBus.Testing framework as documented here:
Example:
[TestFixture]
public class Tests
{
[Test]
public void Run()
{
Test.Initialize();
Test.Handler<MyMessageHandler>()
.SetIncomingHeader("Test", "abc")
.ExpectReply<ResponseMessage>(m => Test.Bus.GetMessageHeader(m, "MyHeaderKey") == "myHeaderValue")
.OnMessage<RequestMessage>(m => m.String = "hello");
}
}
class MyMessageHandler : IHandleMessages<RequestMessage>
{
public IBus Bus { get; set; }
public void Handle(RequestMessage message)
{
ResponseMessage responseMessage = new ResponseMessage();
Bus.SetMessageHeader(responseMessage, "MyHeaderKey", "myHeaderValue");
Bus.Reply(responseMessage);
}
}
Please note, this is for NServiceBus v4/v5. For other versions visit the documentation.
Upvotes: 0
Reputation: 11957
As alluded to in Why NServiceBus OutgoingHeaders is static and not ThreadStatic?, you can define your own GetHeaderAction, which is called when unit testing your project, when NSB is obviously not running.
ExtensionMethods.GetHeaderAction =
((msg, key) => this.Manager.GetHeader(msg, key));
Upvotes: 2