Customize the AWS Message Processing Framework for .NET
Note
This is prerelease documentation for a feature in preview release. It is subject to change.
The AWS Message Processing Framework for .NET builds, sends, and handles messages in three different "layers":
-
At the outermost layer, the framework builds the AWS-native request or response specific to a service. With Amazon SQS for example, it builds
SendMessage
requests, and works with theMessage
objects that are defined by the service. -
Inside the SQS request and response, the framework sets the
MessageBody
element (orMessage
for Amazon SNS orDetail
for Amazon EventBridge) to a JSON-formatted CloudEvent. This contains metadata set by the framework that is accessible on the MessageEnvelope
object when handling a message. -
At the innermost layer, the
data
attribute inside the CloudEvent JSON object contains a JSON serialization of the .NET object that was sent or received as the message.{ "id":"b02f156b-0f02-48cf-ae54-4fbbe05cffba", "source":"/aws/messaging", "specversion":"1.0", "type":"Publisher.Models.ChatMessage", "time":"2023-11-21T16:36:02.8957126+00:00", "data":"<the ChatMessage object serialized as JSON>" }
You can customize how the message envelope is configured and read:
-
"id"
uniquely identifies the message. By default it is set to a new GUID, but this can be overridden by implementing your ownIMessageIdGenerator
and injecting that into the DI container. -
"type"
controls how the message is routed to handlers. By default this uses the full name of the .NET type that corresponds to the message. You can override this via themessageTypeIdentifier
parameter when mapping the message type to the destination viaAddSQSPublisher
,AddSNSPublisher
, orAddEventBridgePublisher
. -
"source"
indicates which system or server sent the message.-
This will be the function name if publishing from AWS Lambda, the cluster name and task ARN if on Amazon ECS, the instance ID if on Amazon EC2, otherwise a fallback value of
/aws/messaging
. -
You can override this via
AddMessageSource
orAddMessageSourceSuffix
on theMessageBusBuilder
.
-
-
"time"
set to the current DateTime in UTC. This can be overridden by implementing your ownIDateTimeHandler
and injecting that into the DI container. -
"data"
contains a JSON representation of the .NET object that was sent or received as the message:-
ConfigureSerializationOptions
onMessageBusBuilder
allows you to configure theSystem.Text.Json.JsonSerializerOptions
that will be used when serializing and deserializing the message. -
To inject additional attributes or transform the message envelope once the framework builds it, you can implement
ISerializationCallback
and register that viaAddSerializationCallback
onMessageBusBuilder
.
-