masstransit

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 11 Imported by: 0

README

envelope-masstransit

MassTransit's JSON message envelope as a go/envelope format, so a Go service on the estate's bus exchanges messages with .NET services that use MassTransit.

Go Reference pipeline

Part of the envelope family. It is documented with its core, at envelope.go.phpboyscout.uk.

go get gitlab.com/phpboyscout/go/envelope-masstransit
f := masstransit.MassTransit() // an envelope.Format

Give it to a go/messaging adapter as the writer to publish to MassTransit consumers, or add it to the reader chain to read what MassTransit producers send. envelope.Read tells it apart from CloudEvents by its content type, application/vnd.masstransit+json.

It refuses, at publish, what MassTransit cannot carry: an ID that is not a UUID, and a payload that is not JSON. sentTime is kept to the .NET tick, 100 ns. What a MassTransit message carries that the event has no place for (correlationId, conversationId, host, the transport's headers) arrives as envelope.Foreign. The full rules are on MassTransit.

The decisions are go/messaging's spec 0014, D2, D9 and D10.

Documentation

Overview

Package masstransit is MassTransit's JSON message envelope as a go/envelope format, so a Go service on the estate's bus can exchange messages with .NET services that use MassTransit.

MassTransit returns the envelope.Format. Give it to an adapter as the writer to publish to MassTransit consumers, or add it to the reader chain to read what MassTransit producers send; envelope.Read tells it apart from CloudEvents by its content type.

The rules it holds an event to, and where each attribute travels, are on MassTransit. The decisions are go/messaging spec 0014's D2, D9 and D10.

Index

Examples

Constants

View Source
const (
	HeaderSubject         = "ce-subject"
	HeaderDataSchema      = "ce-dataschema"
	HeaderDataContentType = "ce-datacontenttype"
)

The headers carrying the attributes MassTransit has no field for.

View Source
const ContentType = "application/vnd.masstransit+json"

ContentType is the media type of MassTransit's JSON envelope.

View Source
const MessageTypePrefix = "urn:message:"

MessageTypePrefix is what MassTransit writes before a message type's name.

View Source
const Precision = 100 * time.Nanosecond

Precision is sentTime's: a .NET tick. Encode truncates to it (D2).

Variables

View Source
var ErrInvalidEnvelope = errors.NewSentinel("masstransit.invalid_envelope",
	"masstransit: the body is not a valid MassTransit JSON envelope")

ErrInvalidEnvelope is returned when a body claimed as MassTransit's is not a MassTransit JSON envelope. Every other refusal is a go/envelope sentinel, so it is counted by the same code whichever format carried the event.

Functions

func MassTransit

func MassTransit() envelope.Format

MassTransit is MassTransit's JSON envelope, as its System.Text.Json serializer writes it, with ContentType as the content type.

It refuses to encode an event MassTransit cannot carry, with envelope.ErrUnrepresentable: an ID that is not a UUID, since messageId is a Guid, and a payload that is not JSON, or whose datacontenttype says it is something else, since message is a JSON value. sentTime is truncated to Precision. The type is written as MessageTypePrefix followed by it, so a .NET consumer of Contracts.OrderCreated wants the type "Contracts:OrderCreated". Subject, dataschema and datacontenttype travel in the envelope's headers, as HeaderSubject and the rest, and so do the extensions.

On read, what the event has no place for is envelope.Foreign: every transport header, every envelope member but the six above (correlationId, conversationId, host and the rest), any messageType after the first, and every envelope header that is not an attribute or an extension. A header named exactly traceparent becomes the extension of that name, and the envelope's wins over the transport's. A header that is null is absent. A member or header named twice is ErrInvalidEnvelope, with the first value kept in the partial event.

An event without data is written with an empty object as its message, because MassTransit refuses a message that is null or absent, so it reads back with {} as its data. A relative source, such as /orders, is written as it is, and arrives in .NET as file:///orders: sourceAddress is a Uri there.

Example

ExampleMassTransit writes an event for a .NET consumer of Contracts.OrderCreated, and reads it back the way a reader chain would.

package main

import (
	"fmt"

	"gitlab.com/phpboyscout/go/envelope"

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

func main() {
	f := masstransit.MassTransit()

	w, err := f.Encode(envelope.Event{
		ID:     "7f2c1a9e-0000-4000-8000-000000000001",
		Source: "rabbitmq://broker/orders",
		Type:   "Contracts:OrderCreated",
		Data:   []byte(`{"order":"4172"}`),
	})
	if err != nil {
		fmt.Println("encode:", err)

		return
	}

	fmt.Println(w.ContentType)

	e, _, format, err := envelope.Read(w, f)
	if err != nil {
		fmt.Println("read:", err)

		return
	}

	fmt.Println(format, e.Type, string(e.Data))
}
Output:
application/vnd.masstransit+json
masstransit Contracts:OrderCreated {"order":"4172"}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL