Skip to content

Exchange a message with a MassTransit consumer

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.

By the end of this you will have written an event that a .NET MassTransit consumer reads as its own message, and read a message MassTransit sent as a Go event. Both directions use programs and captured messages from the module's repository, so the .NET half runs in a container and needs nothing installed beyond Docker. It does need network, to restore MassTransit 8 from NuGet.

1. Get the module and its test programs

$ go get gitlab.com/phpboyscout/go/envelope
$ go get gitlab.com/phpboyscout/go/envelope-masstransit
$ git clone https://gitlab.com/phpboyscout/go/envelope-masstransit

The clone holds testdata/consumer, a small .NET program that reads message bodies with MassTransit's own SystemTextJsonMessageSerializer, as a consumer of OrderCreated would. It is not part of the Go module, and the Go module does not depend on MassTransit. It references MassTransit 8, which is the version this format was captured from.

2. Write a message from Go

The consumer declares OrderCreated in the .NET namespace Capture, so the event's type is Capture:OrderCreated: MassTransit names a message type by namespace and class, joined with a colon.

package main

import (
    "log"
    "os"
    "path/filepath"
    "time"

    "gitlab.com/phpboyscout/go/envelope"
    masstransit "gitlab.com/phpboyscout/go/envelope-masstransit"
)

func main() {
    w, err := masstransit.MassTransit().Encode(envelope.Event{
        ID:     "7f2c1a9e-0000-4000-8000-000000000001",
        Source: "rabbitmq://broker/orders",
        Type:   "Capture:OrderCreated",
        Time:   time.Date(2026, 10, 24, 6, 0, 14, 0, time.UTC),
        Data:   []byte(`{"order":"4172","lines":3}`),
    })
    if err != nil {
        log.Fatal(err)
    }

    if err := os.WriteFile(filepath.Join("out", "order.json"), w.Body, 0o644); err != nil {
        log.Fatal(err)
    }
}

Create out/ first. The ID is a UUID because MassTransit's messageId is a Guid, and the source is an absolute URI because sourceAddress is a Uri. The file holds one line:

{"messageId":"7f2c1a9e-0000-4000-8000-000000000001","sourceAddress":"rabbitmq://broker/orders","messageType":["urn:message:Capture:OrderCreated"],"message":{"order":"4172","lines":3},"sentTime":"2026-10-24T06:00:14.0000000Z","headers":{}}

Encode wrote only the body. The content type, application/vnd.masstransit+json, is w.ContentType, and the transport is responsible for carrying it.

3. Have MassTransit read it

From the clone, with $DIR the absolute path of the directory holding order.json:

$ docker run --rm -v "$PWD/testdata/consumer:/src" -v "$DIR:/in" -w /src \
    mcr.microsoft.com/dotnet/sdk:9.0 dotnet run -- /in

The program reads every .json file in /in and prints, per file, what MassTransit's deserializer made of it. Its lines are messageId, sourceAddress, sentTime, supported (the message types the envelope lists), isOrderCreated (whether the message can be consumed as an OrderCreated), message (the deserialized OrderCreated) and a header: line for each header. If MassTransit refuses the envelope the program prints an ERROR: line with the exception instead.

Check that messageId is your ID, that supported lists urn:message:Capture:OrderCreated, and that isOrderCreated is true. That is a .NET consumer reading what Go wrote.

Output not reproduced

This page does not quote the program's output, because it was written without running the .NET side. Run it and compare with the three checks above.

4. Read a message MassTransit sent

testdata/full.txt is a message MassTransit 8.5.10 sent itself over its in-memory transport, as the producer program prints it: the content type, each transport header, then the body after a blank line. Read it back with envelope.Read:

raw, err := os.ReadFile("testdata/full.txt")
if err != nil {
    log.Fatal(err)
}

head, body, _ := strings.Cut(string(raw), "\n\n")
wire := envelope.Wire{Headers: map[string][]string{}, Body: []byte(body)}

for line := range strings.SplitSeq(head, "\n") {
    switch {
    case strings.HasPrefix(line, "content-type: "):
        wire.ContentType = strings.TrimPrefix(line, "content-type: ")
    case strings.HasPrefix(line, "header "):
        k, v, _ := strings.Cut(strings.TrimPrefix(line, "header "), ": ")
        wire.Headers[k] = append(wire.Headers[k], v)
    }
}

e, foreign, format, err := envelope.Read(wire, masstransit.MassTransit())

Printing the results gives:

format: masstransit
id: 7f2c1a9e-0000-4000-8000-000000000002
type: Capture:OrderCreated
source: loopback://localhost/d1898bdd12de_mtcapture_bus_kwyyyy8ox93wde86bdxudfy1bw
time: 2026-10-24T06:00:14.2212896Z
extensions: map[traceparent:00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01]
correlationId: [7f2c1a9e-0000-4000-8000-0000000000c0]
Tenant: [continuity]
conflicts: []
  • The type lost its prefix. MassTransit wrote urn:message:Capture:OrderCreated.
  • traceparent is an extension. The producer set it as a header, and the format promotes that one name.
  • Tenant and correlationId are foreign. The event has no place for a correlation ID, and Tenant is not a valid extension name because it has a capital.
  • There are no conflicts. MassTransit mirrors MessageId, Tenant and traceparent onto the transport, with the same values.

Where next