Azure.Messaging.EventGrid.SystemEvents 1.0.0-beta.5
Azure Event Grid System Events client library for .NET
Azure Event Grid allows you to build applications with event-based architectures. The Event Grid service fully manages all routing of events from any source to any destination, for any application. Azure service events and custom events can be published directly to the service, where the events can then be filtered and sent to various recipients, such as built-in handlers or custom webhooks. To learn more about Azure Event Grid: What is Event Grid?
Use the client library for Azure Event Grid System Events to:
Deserialize Event Grid system events into strongly typed models.
Source code | Package (NuGet) | API reference documentation | Product documentation
Getting started
Install the package
Install the client library for .NET with NuGet:
dotnet add package Azure.Messaging.EventGrid.SystemEvents --prerelease
Prerequisites
You must have an Azure subscription and an Azure resource group with a custom Event Grid topic or domain. Follow this step-by-step tutorial to register the Event Grid resource provider and create Event Grid topics using the Azure portal. There is a similar tutorial using Azure CLI.
Receiving and Deserializing Events
There are several different Azure services that act as event handlers.
Note: if using Webhooks for event delivery of the Event Grid schema, Event Grid requires you to prove ownership of your Webhook endpoint before it starts delivering events to that endpoint. At the time of event subscription creation, Event Grid sends a subscription validation event to your endpoint, as seen below. Learn more about completing the handshake here: Webhook event delivery. For the CloudEvents schema, the service validates the connection using the HTTP options method. Learn more here: CloudEvents validation.
Once events are delivered to the event handler, we can deserialize the JSON payload into a list of events.
var bytes = await httpContent.ReadAsByteArrayAsync();
// Parse the JSON payload into a list of events
CloudEvent[] cloudEvents = CloudEvent.ParseMany(new BinaryData(bytes));
Deserializing event data
Deserializing using ToObjectFromJson<T>()
:
From here, one can access the event data by deserializing to a specific type by calling ToObjectFromJson<T>()
on the Data
property. In order to deserialize to the correct type, the Type
property helps distinguish between different events. Custom event data should be deserialized using the generic method ToObjectFromJson<T>()
. There is also an extension method ToObject<T>()
that accepts a custom ObjectSerializer
to deserialize the event data.
foreach (CloudEvent cloudEvent in cloudEvents)
{
switch (cloudEvent.Type)
{
case "Contoso.Items.ItemReceived":
// By default, ToObjectFromJson<T> uses System.Text.Json to deserialize the payload
ContosoItemReceivedEventData itemReceived = cloudEvent.Data.ToObjectFromJson<ContosoItemReceivedEventData>();
Console.WriteLine(itemReceived.ItemSku);
break;
case "MyApp.Models.CustomEventType":
// One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly
TestPayload testPayload = cloudEvent.Data.ToObject<TestPayload>(myCustomSerializer);
Console.WriteLine(testPayload.Name);
break;
case SystemEventNames.StorageBlobDeleted:
// Example for deserializing system events using ToObjectFromJson<T>
StorageBlobDeletedEventData blobDeleted = cloudEvent.Data.ToObjectFromJson<StorageBlobDeletedEventData>();
Console.WriteLine(blobDeleted.BlobType);
break;
}
}
Deserializing using TryGetSystemEventData()
:
If expecting mostly system events, it may be cleaner to switch on TryGetSystemEventData()
and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null.
As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of TryGetSystemEventData
would change from false
to true
. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic ToObjectFromJson<T>
method on the Data
property so that your code flow doesn't change automatically after upgrading (of course, you may still want to modify your code to consume the newly released system event model as opposed to your custom model).
foreach (CloudEvent cloudEvent in cloudEvents)
{
// If the event is a system event, TryGetSystemEventData will return the deserialized system event
if (cloudEvent.TryGetSystemEventData(out object systemEvent))
{
switch (systemEvent)
{
case SubscriptionValidationEventData subscriptionValidated:
Console.WriteLine(subscriptionValidated.ValidationCode);
break;
case StorageBlobCreatedEventData blobCreated:
Console.WriteLine(blobCreated.BlobType);
break;
// Handle any other system event type
default:
Console.WriteLine(cloudEvent.Type);
// we can get the raw Json for the event using Data
Console.WriteLine(cloudEvent.Data.ToString());
break;
}
}
else
{
switch (cloudEvent.Type)
{
case "MyApp.Models.CustomEventType":
TestPayload deserializedEventData = cloudEvent.Data.ToObjectFromJson<TestPayload>();
Console.WriteLine(deserializedEventData.Name);
break;
// Handle any other custom event type
default:
Console.Write(cloudEvent.Type);
Console.WriteLine(cloudEvent.Data.ToString());
break;
}
}
}
Key concepts
Thread safety
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Additional concepts
Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime
Examples
You can familiarize yourself with different APIs using Samples.
Troubleshooting
Describe common errors and exceptions, how to "unpack" them if necessary, and include guidance for graceful handling and recovery.
Provide information to help developers avoid throttling or other service-enforced errors they might encounter. For example, provide guidance and examples for using retry or connection policies in the API.
If the package or a related package supports it, include tips for logging or enabling instrumentation to help them debug their code.
Next steps
View more https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/eventgrid/Azure.Messaging.EventGrid.Namespaces/samples here for common usages of the Event Grid client library: Event Grid Samples.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit Contributor License Agreements.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Showing the top 20 packages that depend on Azure.Messaging.EventGrid.SystemEvents.
Packages | Downloads |
---|---|
Azure.Messaging.EventGrid
This library can be used to publish events to Azure Event Grid and to consume events delivered by EventGrid. It also defines the event schemas for the events published to EventGrid by various Azure services.
|
2 |
Version | Downloads | Last updated |
---|---|---|
1.0.0-beta.5 | 1 | 06/10/2025 |
1.0.0-beta.4 | 0 | 06/03/2025 |
1.0.0-beta.3 | 0 | 05/19/2025 |
1.0.0-beta.2 | 0 | 02/21/2025 |
1.0.0-beta.1 | 0 | 06/20/2024 |