diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index bc4061412c43..f84574a8a980 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -127,21 +127,35 @@ await client.SendEventsAsync(eventsList); ### Publish CloudEvents to an Event Grid Topic Publishing events to Event Grid is performed using the `EventGridPublisherClient`. Use the provided `SendEvents`/`SendEventsAsync` method to publish events to the topic. ```C# Snippet:SendCloudEventsToTopic +// Example of a custom ObjectSerializer used to serialize the event payload to JSON +var myCustomDataSerializer = new JsonObjectSerializer( + new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + // Add CloudEvents to a list to publish to the topic List eventsList = new List { - // CloudEvent with populated data + // CloudEvent with custom model serialized to JSON + new CloudEvent( + "/cloudevents/example/source", + "Example.EventType", + new CustomModel() { A = 5, B = true }), + + // CloudEvent with custom model serialized to JSON using a custom serializer new CloudEvent( "/cloudevents/example/source", "Example.EventType", - myCustomDataSerializer.Serialize("This is the event data")), + myCustomDataSerializer.Serialize(new CustomModel() { A = 5, B = true }), + "application/json"), // CloudEvents also supports sending binary-valued data new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData(Encoding.UTF8.GetBytes("This is binary data")), - "example/binary")}; + new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), + "application/octet-stream")}; // Send the events await client.SendEventsAsync(eventsList); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md index 280601e0a41f..0a79ac23f89b 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md @@ -66,21 +66,35 @@ Following that, invoke `SendEvents` or `SendEventsAsync` to publish the events t Note on `CloudEvent`: each `CloudEvent` has a set of required, non-nullable properties. However, `Data` is *not required*. `Time` and `SpecVersion` are required properties that are set by default, but can also be manually set if needed. `Time` is also set by default, but not required. ```C# Snippet:SendCloudEventsToTopic +// Example of a custom ObjectSerializer used to serialize the event payload to JSON +var myCustomDataSerializer = new JsonObjectSerializer( + new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + // Add CloudEvents to a list to publish to the topic List eventsList = new List { - // CloudEvent with populated data + // CloudEvent with custom model serialized to JSON + new CloudEvent( + "/cloudevents/example/source", + "Example.EventType", + new CustomModel() { A = 5, B = true }), + + // CloudEvent with custom model serialized to JSON using a custom serializer new CloudEvent( "/cloudevents/example/source", "Example.EventType", - myCustomDataSerializer.Serialize("This is the event data")), + myCustomDataSerializer.Serialize(new CustomModel() { A = 5, B = true }), + "application/json"), // CloudEvents also supports sending binary-valued data new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData(Encoding.UTF8.GetBytes("This is binary data")), - "example/binary")}; + new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), + "application/octet-stream")}; // Send the events await client.SendEventsAsync(eventsList); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs index 2e8c2e19e9af..9b909ea4bb5d 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs @@ -8,9 +8,6 @@ using System.Threading.Tasks; using Azure.Core.Serialization; using Azure.Core.TestFramework; -using Azure.Messaging; -using Azure.Messaging.EventGrid; -using Azure.Messaging.EventGrid.Tests; using NUnit.Framework; namespace Azure.Messaging.EventGrid.Tests.Samples @@ -90,38 +87,44 @@ public async Task SendCloudEventsToTopic() string topicEndpoint = TestEnvironment.CloudEventTopicHost; string topicAccessKey = TestEnvironment.CloudEventTopicKey; - // Example of a custom ObjectSerializer used to serialize the event payload to JSON - var myCustomDataSerializer = new JsonObjectSerializer( - new JsonSerializerOptions() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); - // Create the publisher client using an AzureKeyCredential // Custom topic should be configured to accept events of the CloudEvents 1.0 schema #region Snippet:CreateClientWithOptions - EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), new AzureKeyCredential(topicAccessKey)); #endregion #region Snippet:SendCloudEventsToTopic + // Example of a custom ObjectSerializer used to serialize the event payload to JSON + var myCustomDataSerializer = new JsonObjectSerializer( + new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + // Add CloudEvents to a list to publish to the topic List eventsList = new List { - // CloudEvent with populated data + // CloudEvent with custom model serialized to JSON new CloudEvent( "/cloudevents/example/source", "Example.EventType", - myCustomDataSerializer.Serialize("This is the event data")), + new CustomModel() { A = 5, B = true }), + + // CloudEvent with custom model serialized to JSON using a custom serializer + new CloudEvent( + "/cloudevents/example/source", + "Example.EventType", + myCustomDataSerializer.Serialize(new CustomModel() { A = 5, B = true }), + "application/json"), // CloudEvents also supports sending binary-valued data new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData(Encoding.UTF8.GetBytes("This is binary data")), - "example/binary")}; + new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), + "application/octet-stream")}; // Send the events await client.SendEventsAsync(eventsList); @@ -162,5 +165,11 @@ public async Task SendEventsToDomain() await client.SendEventsAsync(eventsList); #endregion } + + internal class CustomModel + { + public int A { get; set; } + public bool B { get; set; } + } } } diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs index f4071a119079..7a672653259b 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs @@ -15,7 +15,7 @@ public partial class EventGridSamples : SamplesBase // Example JSON payloads private readonly string jsonPayloadSampleOne = "[{ \"id\": \"2d1781af-3a4c\", \"topic\": \"/examples/test/payload\", \"subject\": \"\", \"data\": { \"Name\": \"example\",\"Age\": 20 },\"eventType\": \"MyApp.Models.CustomEventType\",\"eventTime\": \"2018-01-25T22:12:19.4556811Z\",\"dataVersion\": \"1\"}]"; - private readonly string jsonPayloadSampleTwo = "[{ \"id\": \"2d1781af-3a4c\", \"source\": \"/examples/test/payload\", \"data\": { \"name\": \"example\",\"age\": 20 },\"type\": \"MyApp.Models.CustomEventType\",\"time\": \"2018-01-25T22:12:19.4556811Z\",\"specversion\": \"1\"}]"; + private readonly string jsonPayloadSampleTwo = "[{ \"id\": \"2d1781af-3a4c\", \"source\": \"/examples/test/payload\", \"data\": { \"name\": \"example\",\"age\": 20 },\"type\": \"MyApp.Models.CustomEventType\",\"time\": \"2018-01-25T22:12:19.4556811Z\",\"specversion\": \"1.0\"}]"; // This sample demonstrates how to parse EventGridEvents from JSON and access event data using AsSystemEventData() [Test]