Table of Contents

Class CloudEventContentTypes

Namespace
Excalibur.Dispatch.CloudEvents
Assembly
Excalibur.Dispatch.dll

Provides content type negotiation and formatting for CloudEvents.

public static class CloudEventContentTypes
Inheritance
CloudEventContentTypes
Inherited Members

Remarks

The encode and decode members on this type serialize payloads REFLECTIVELY, and are annotated accordingly. They construct a parameterless CloudEvents JSON formatter, which resolves payload type information at run time. Under trimming or an ahead-of-time publish that information may not exist, and the failure surfaces at run time on the consumer's machine rather than at their build — so the annotation is what makes the cost visible where it can still be acted on.

The reflection-free path is CloudEventSerializer. Register it with the AddCloudEvents overload taking an IJsonTypeInfoResolver — typically a source-generated JsonSerializerContext covering the types published as CloudEvent data — and serialize through the resolved instance. That overload carries no annotation, which is the whole point of the split.

The content-type members on this type — the constants, NegotiateContentType(string?, bool) and CreateContentTypeHeader(string, string?, Dictionary<string, string>?) — are pure string handling and are deliberately NOT annotated.

Fields

ApplicationJson

Standard JSON content type.

public const string ApplicationJson = "application/json"

Field Value

string

ApplicationOctetStream

Binary content type for CloudEvents in HTTP binary mode.

public const string ApplicationOctetStream = "application/octet-stream"

Field Value

string

CloudEventsBatchJson

CloudEvents batch JSON format content type.

public const string CloudEventsBatchJson = "application/cloudevents-batch+json"

Field Value

string

CloudEventsJson

CloudEvents JSON format content type.

public const string CloudEventsJson = "application/cloudevents+json"

Field Value

string

Methods

CreateContentTypeHeader(string, string?, Dictionary<string, string>?)

Creates a content type header with additional parameters.

public static string CreateContentTypeHeader(string mediaType, string? charset = "utf-8", Dictionary<string, string>? parameters = null)

Parameters

mediaType string
charset string
parameters Dictionary<string, string>

Returns

string

Deserialize(byte[], string, params CloudEventAttribute[])

Deserializes a CloudEvent from bytes with the specified content type.

[RequiresUnreferencedCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
[RequiresDynamicCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
public static CloudEvent Deserialize(byte[] data, string contentType, params CloudEventAttribute[] extensionAttributes)

Parameters

data byte[]
contentType string
extensionAttributes CloudEventAttribute[]

Returns

CloudEvent

DeserializeBatch(byte[], string, params CloudEventAttribute[])

Deserializes a CloudEvent batch from bytes.

[RequiresUnreferencedCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
[RequiresDynamicCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
public static IReadOnlyList<CloudEvent> DeserializeBatch(byte[] data, string contentType = "application/cloudevents-batch+json", params CloudEventAttribute[] extensionAttributes)

Parameters

data byte[]
contentType string
extensionAttributes CloudEventAttribute[]

Returns

IReadOnlyList<CloudEvent>

Exceptions

ArgumentException

GetFormatter(string)

Gets the appropriate formatter for a content type.

[RequiresUnreferencedCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
[RequiresDynamicCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
public static JsonEventFormatter GetFormatter(string contentType)

Parameters

contentType string

Returns

JsonEventFormatter

Exceptions

NotSupportedException

NegotiateContentType(string?, bool)

Determines the best content type based on Accept headers.

public static string NegotiateContentType(string? acceptHeader, bool supportsBatch = false)

Parameters

acceptHeader string
supportsBatch bool

Returns

string

Serialize(CloudEvent, string)

Serializes a CloudEvent to bytes with the specified content type.

[RequiresUnreferencedCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
[RequiresDynamicCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
public static byte[] Serialize(CloudEvent cloudEvent, string contentType)

Parameters

cloudEvent CloudEvent
contentType string

Returns

byte[]

SerializeBatch(CloudEventBatch, string)

Serializes a CloudEvent batch to bytes.

[RequiresUnreferencedCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
[RequiresDynamicCode("CloudEvent payloads are serialized reflectively on this path. Use the registered CloudEventSerializer, built by AddCloudEvents(IJsonTypeInfoResolver) over a source-generated context covering the types you publish as CloudEvent data.")]
public static byte[] SerializeBatch(CloudEventBatch batch, string contentType = "application/cloudevents-batch+json")

Parameters

batch CloudEventBatch
contentType string

Returns

byte[]

Exceptions

ArgumentException