Skip to content

Instantly share code, notes, and snippets.

@awswithdotnet
Created March 8, 2022 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awswithdotnet/4ed2f060de93195e8a937626198fe76b to your computer and use it in GitHub Desktop.
Save awswithdotnet/4ed2f060de93195e8a937626198fe76b to your computer and use it in GitHub Desktop.
sns CloudEventsMessageFormatting CloudEventsMessageFormatter Complete
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