Skip to content

Headers

David Brower edited this page Apr 13, 2016 · 2 revisions

Headers as Metadata

In the majority of message-based system a message consists of a header and a body. Whereas the body contains the application-specific data i.e. the payload, the header contains information about the message itself i.e. metadata.

A message header may contain a correlation id, a return address, an expiration duration, or any other "fields that are used by the messaging infrastructure to manage the flow of messages" (Gregor Hohpe).

In EventStore, events, being messages, contain headers which can be set and read by the plugin.

Setting Headers

To set the headers when publishing an event to EventStore we pass in a delegate in which we can append items to an dictionary:

public async Task DoSomething()
{
    ...
    await EventStoreRepository.PublishAsync(new SomethingHappened(), "targetstream",
                headers =>
                {
                    headers.Add("CorrelationId", correlationId);
                    headers.Add("SitarPlayer", "Ustad Vilyat Khan");
                });
}

Reading Headers

Headers for an event can be retrieved through the Headers property of the Request object that ServiceStack exposes in any class that inherits from Service:

public class PurchaseOrderService: Service
{
    public object Any(PurchaseOrderCreated @event)
    {
        var correlationId = Request.Headers["CorrelationId"];
        //handle event
    }
}