Skip to content

Read from NServiceBus

Note

NServiceBus licensing. NServiceBus is dual-licensed: the Reciprocal Public License 1.5, or Particular Software's commercial agreement (particular.net/LicenseAgreement). This format was captured from NServiceBus 10.2.9, and only the module's own .NET test programs use it. The Go module itself does not depend on NServiceBus.

Add it to the chain

readers := []envelope.Format{
    cloudevents.Binary(cloudevents.HTTP),
    cloudevents.Structured(),
    nservicebus.NServiceBus(),
}

e, foreign, format, err := envelope.Read(w, readers...)

Read tells NServiceBus apart from the others by its headers: the format claims a message that has an NServiceBus.MessageId header with a value, in any case. A canonicalised Nservicebus.Messageid, as net/http writes, is claimed too. A bare MessageId, which is MassTransit's transport mirror, is not. A message that is also claimed by another format is ErrAmbiguous.

What you get

For a bare Send, testdata/send.txt:

  • e.ID is NServiceBus.MessageId, e.Source is NServiceBus.OriginatingEndpoint.
  • e.Type is NServiceBus.EnclosedMessageTypes, and for a message sent by NServiceBus that is the assembly-qualified name, Shop.Messages.PlaceOrder, producer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. Match on the part you need.
  • e.DataContentType is NServiceBus.ContentType.
  • e.Time is NServiceBus.TimeSent, to the microsecond.
  • e.Data is the body, untouched.
  • foreign.Headers holds NServiceBus's own other headers, such as NServiceBus.ConversationId.

Reply in the format Read returned.

Decode the payload

Pick the decoder from e.DataContentType:

if e.DataContentType == "application/json" {
    var order Order
    if err := json.Unmarshal(e.Data, &order); err != nil {
        return err
    }
}

NServiceBus's SystemJsonSerializer wrote the fixtures with Pascal-case property names ("Order", "Lines"), so a struct needs json tags for them.

A message with several types

When NServiceBus.EnclosedMessageTypes lists the types a message implements, separated by ;, the first is e.Type and the whole header value is in foreign.Headers["NServiceBus.EnclosedMessageTypes"].

all := foreign.Headers["NServiceBus.EnclosedMessageTypes"] // one entry: the whole list

Mind the extension rule

Any lower-case header with a valid extension name, such as priority, is an extension, whoever set it. A header of that kind with an empty value, or a control character in it, makes the whole message fail Validate. See Carry headers and foreign fields.

A message it cannot read

A message with no NServiceBus.EnclosedMessageTypes or no NServiceBus.OriginatingEndpoint, or with a TimeSent that does not match the format, is refused and the partial event is returned. See Handle what is refused.