Producing messages with the .NET SDK

Last updated:

Build the canonical envelope with EnvelopeCodec.Make, encode it to JSON with EnvelopeCodec.Encode, then publish the bytes with your own broker client.

using BabelQueue;

var env = EnvelopeCodec.Make(
    "urn:babel:orders:created",
    new Dictionary<string, object?> { ["order_id"] = 1042L, ["amount"] = 99.90 },
    queue: "orders");

string body = EnvelopeCodec.Encode(env); // compact UTF-8 JSON
// await db.ListRightPushAsync("queues:orders", body);
//   /  channel.BasicPublish("", "orders", props, Encoding.UTF8.GetBytes(body));

Encode produces the canonical schema_version: 1 envelope, with meta.lang set to "dotnet":

{
  "job": "urn:babel:orders:created",
  "trace_id": "7b3f9c2a-e41d-4f88-9b2a-1c0d5e6f7a8b",
  "data": { "order_id": 1042, "amount": 99.90 },
  "meta": { "id": "f1e2d3c4-b5a6-4789-90ab-cdef01234567", "queue": "orders", "lang": "dotnet", "schema_version": 1, "created_at": 1749132727000 },
  "attempts": 0
}

Extension (meta) keys

A Make overload takes a meta dictionary for your own meta entries:

var env = EnvelopeCodec.Make(
    "urn:acme:orders:created",
    new Dictionary<string, object?> { ["order_id"] = 1042L },
    queue: "orders",
    traceId: null,
    meta: new Dictionary<string, object?>
    {
        ["ext"] = new Dictionary<string, object?>
        {
            ["acme"] = new Dictionary<string, object?> { ["tier"] = "gold" },
        },
    });

The overload takes all five parameters; the four-parameter Make(urn, data, queue, traceId) keeps its exact signature. Make and Encode throw BabelQueueException for a key that collides with a core meta key (id, queue, lang, schema_version, created_at) or a forbidden one (max_retries, attempts, source, ts). Encode also throws for a top-level timestamp or urn left on the envelope.

Keep vendor data under a single meta.ext.<vendor> block and use snake_case keys (ADR-0031). The entries are written next to the core meta keys and survive every retry, dead-letter and redrive hop. See Extension keys in the envelope spec.

Next: Consuming messages.