Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions sdk/eventgrid/Azure.Messaging.EventGrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<CloudEvent> eventsList = new List<CloudEvent>
{
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CloudEvent> eventsList = new List<CloudEvent>
{
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<CloudEvent> eventsList = new List<CloudEvent>
{
// 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);
Expand Down Expand Up @@ -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; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class EventGridSamples : SamplesBase<EventGridTestEnvironment>
// 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]
Expand Down