Write an event several ways and read it back¶
By the end of this you will have written one event as CloudEvents binary, CloudEvents structured,
MassTransit and NServiceBus, read all four with a single call, and seen what changes between them.
It needs no transport: Encode returns the bytes and headers, and Read takes them back.
1. Install¶
$ go get gitlab.com/phpboyscout/go/envelope
$ go get gitlab.com/phpboyscout/go/envelope-cloudevents
$ go get gitlab.com/phpboyscout/go/envelope-masstransit
$ go get gitlab.com/phpboyscout/go/envelope-nservicebus
The first is the event and the Format seam. Each of the others is one format, and needs nothing
but the first.
2. Write the program¶
package main
import (
"fmt"
"log"
"time"
"gitlab.com/phpboyscout/go/envelope"
cloudevents "gitlab.com/phpboyscout/go/envelope-cloudevents"
masstransit "gitlab.com/phpboyscout/go/envelope-masstransit"
nservicebus "gitlab.com/phpboyscout/go/envelope-nservicebus"
)
func main() {
e := envelope.Event{
ID: "7f2c1a9e-0000-4000-8000-000000000001",
Source: "Shop.Sales",
Type: "Shop.Messages.PlaceOrder",
Subject: "order-4172",
Time: time.Date(2026, 10, 24, 8, 27, 24, 123456789, time.UTC),
DataContentType: "application/json",
Extensions: map[string]string{"tenant": "continuity"},
Data: []byte(`{"Order":"4172"}`),
}
readers := []envelope.Format{
cloudevents.Binary(cloudevents.NATS),
cloudevents.Structured(),
masstransit.MassTransit(),
nservicebus.NServiceBus(),
}
for _, writer := range readers {
w, err := writer.Encode(e)
if err != nil {
log.Fatal(err)
}
got, _, format, err := envelope.Read(w, readers...)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%-14v %-34q %s %s\n", format, w.ContentType, got.Time.Format(time.RFC3339Nano), got.Type)
}
}
SpecVersion is absent from the event on purpose: every format fills it in, because there is one
value it can have. The ID is a UUID because MassTransit requires one, and the data is JSON for the
same reason. Part 4 breaks the ID.
3. Run it¶
$ go run .
binary(ce-) "application/json" 2026-10-24T08:27:24.123456789Z Shop.Messages.PlaceOrder
structured "application/cloudevents+json" 2026-10-24T08:27:24.123456789Z Shop.Messages.PlaceOrder
masstransit "application/vnd.masstransit+json" 2026-10-24T08:27:24.1234567Z Shop.Messages.PlaceOrder
nservicebus "application/json" 2026-10-24T08:27:24.123456Z Shop.Messages.PlaceOrder
Three things are worth seeing here.
Readreturned the format that wrote each message. The reader was given all four and chose by what each one claims, not by the order of the list.- The type came back whole in all four, though MassTransit writes it as
urn:message:Shop.Messages.PlaceOrderand NServiceBus in a header of its own. A format puts the event where its envelope keeps it and takes it back out. - The time got shorter. CloudEvents keeps the nanoseconds. MassTransit keeps 100 ns, a .NET tick.
NServiceBus keeps microseconds. A format may lower the precision of
Timeto its own, and nothing else.
4. Make it refuse¶
Change the ID to order-1 and run it again. CloudEvents carries any string, so the first two lines
print as before. The third format is MassTransit, and the program stops there:
$ go run .
binary(ce-) "application/json" 2026-10-24T08:27:24.123456789Z Shop.Messages.PlaceOrder
structured "application/cloudevents+json" 2026-10-24T08:27:24.123456789Z Shop.Messages.PlaceOrder
<date> <time> id "order-1": messageId is a Guid: envelope: the format cannot represent this event
MassTransit's messageId is a .NET Guid, so Encode returns
ErrUnrepresentable to the publisher rather than
sending something the consumer would reject. A format never invents an ID to get round this.
5. Make it ambiguous¶
Add a ce-specversion header to a MassTransit message and read it with all four formats:
w, _ := masstransit.MassTransit().Encode(e)
w.Headers = map[string][]string{"ce-specversion": {"1.0"}}
_, _, _, err := envelope.Read(w, readers...)
fmt.Println(err)
Binary CloudEvents claims it by the header and MassTransit by the content type. Read decodes it
with neither, because either answer could be the wrong event. See
Why Read asks every format.
Where next¶
- Read more than one format: the same call, in a service.
- The Format seam: what each method must do.
- The adapters: CloudEvents, MassTransit and NServiceBus.