Skip to content

Write to MassTransit

Warning

MassTransit 9 is commercially licensed. MassTransit 9 and later is under the MassTransit Commercial Software License Agreement (massient.com/license), a paid subscription with no exemption for testing. This format was captured from MassTransit 8, which is Apache-2.0, and the module's own .NET test programs stay on 8.x (envelope-masstransit#1). The Go module itself does not depend on MassTransit.

Choose the format as the writer

masstransit.MassTransit() is an envelope.Format. Give it to the adapter as the one format every event is published with. With go/messaging-rabbitmq, that is Settings.Format:

rabbitmq.New(rabbitmq.Settings{
    URL: url, Exchange: "events",
    Format: masstransit.MassTransit(),
    Reads:  []envelope.Format{cloudevents.Binary(cloudevents.HTTP), cloudevents.Structured(), masstransit.MassTransit()},
})

Reads is the chain a received message is read with. Leaving MassTransit out of it means what MassTransit sends back is ErrUnrecognised. See Read more than one format. The adapter writes Wire.ContentType on the message's native content type, and MassTransit's Encode writes no headers. That the adapter's README shows this setting is the source here; the broker topology a MassTransit consumer expects is not covered by this module.

Shape the event for a .NET consumer

Attribute Set it to
ID A UUID, such as 7f2c1a9e-0000-4000-8000-000000000001, with dashes and no braces.
Type The consumer's namespace and class joined with a colon: Contracts:OrderCreated for Contracts.OrderCreated. The format adds urn:message:.
Source An absolute URI. A relative one is written as it is and arrives in .NET as file:///orders.
Data JSON, with DataContentType empty, application/json, text/json or a +json type.

An event without data is written with {} as its message, because MassTransit refuses a message that is null or absent. A consumer sees an empty object, not a missing one.

Time is written to 100 ns. Finer is truncated, never refused.

Handle a refusal at publish

Encode, and so a publish, returns ErrUnrepresentable for an event MassTransit cannot carry, with the field named:

_, err := f.Encode(e)
if errors.Is(err, envelope.ErrUnrepresentable) {
    // the event is valid; this format cannot carry it
}

See Handle what MassTransit refuses.