Created
March 8, 2022 18:09
-
-
Save awswithdotnet/4ed2f060de93195e8a937626198fe76b to your computer and use it in GitHub Desktop.
sns CloudEventsMessageFormatting CloudEventsMessageFormatter Complete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using CloudNative.CloudEvents; | |
using Abstractions; | |
using CloudNative.CloudEvents.NewtonsoftJson; | |
namespace CloudEventsMessageFormatting | |
{ | |
public class CloudEventsMessageFormatter<TIn> : IMessageFormatter<TIn, String> | |
{ | |
public String Format(TIn message) | |
{ | |
CloudEvent cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0) | |
{ | |
DataContentType = "text/plain", | |
Data = message, | |
Type = "test.type", | |
Source = new Uri("urn:a.b.c"), | |
Id = Guid.NewGuid().ToString(), | |
Time = DateTime.Now | |
}; | |
var jsonFormatter = new JsonEventFormatter(); | |
var eventMessage = jsonFormatter.EncodeStructuredModeMessage(cloudEvent, out var contentType); | |
string encodedMessage = System.Text.Encoding.UTF8.GetString(eventMessage.ToArray(), 0, eventMessage.Length); | |
return encodedMessage; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment