Version 4 (V4) of the Amazon SDK for .NET has been released!
To start using the new version of the SDK, see the Amazon SDK for .NET (V4) Developer Guide, especially the topic for Migrating to version 4.
Customize the Amazon Message Processing Framework for .NET
The Amazon Message Processing Framework for .NET builds, sends, and handles messages in three different "layers":
-
At the outermost layer, the framework builds the Amazon-native request or response specific to a service. With Amazon SQS for example, it builds
SendMessagerequests, and works with theMessageobjects that are defined by the service. -
Inside the SQS request and response, the framework sets the
MessageBodyelement (orMessagefor Amazon SNS orDetailfor Amazon EventBridge) to a JSON-formatted CloudEvent. This contains metadata set by the framework that is accessible on the MessageEnvelopeobject when handling a message. -
At the innermost layer, the
dataattribute 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 ownIMessageIdGeneratorand 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 themessageTypeIdentifierparameter 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 Amazon 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
AddMessageSourceorAddMessageSourceSuffixon theMessageBusBuilder.
-
-
"time"set to the current DateTime in UTC. This can be overridden by implementing your ownIDateTimeHandlerand injecting that into the DI container. -
"data"contains a JSON representation of the .NET object that was sent or received as the message:-
ConfigureSerializationOptionsonMessageBusBuilderallows you to configure theSystem.Text.Json.JsonSerializerOptionsthat 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
ISerializationCallbackand register that viaAddSerializationCallbackonMessageBusBuilder.
-