Deserializing Events
#
Deserializing EventsThe Arcus.EventGrid
package provides several ways to deserializing events.
Following paragraphs describe each supported way to deserialize an event.
#
Deserializing Built-In Azure EventsWhen using official Azure events, you can use .ParseFromData<>
to deserialize then those based on the built-in types as shown in the example:
using Arcus.EventGrid;using Arcus.EventGrid.Parsers;using Microsoft.Azure.EventGrid.Models;
// Parse directly from an event data type with the `.Parse` function.byte[] rawEvent = ...EventBatch<Event> eventBatch = EventParser.Parse(rawEvent);
// The type `EventGridEvent` comes directly from the official SDK package// and can be cast implicitly like so,EventGridEvent eventGridEvent = eventBatch.Events.Single();
// Or explicitly.EventGridEvent eventGridEvent = eventBatch.Events.Single().AsEventGridEvent();
// The actual EventGrid payload can be retrieved by passing along the Azure SDK model type.var storageEventData = eventGridEvent.GetPayload<StorageBlobCreatedEventData>();
You can find a list of supported built-in Azure events in the official documentation.
#
Deserializing CloudEvent EventsWe provide support for deserializing CloudEvents using the official SDK for C#.
Upon receiving of CloudEvent events:
using Arcus.EventGrid;using Arcus.EventGrid.Parsers;using CloudNative.CloudEvents;
string cloudEventJson = ...EventBatch<Event> eventBatch = EventParser.Parse(cloudEventJson);var events = eventBatch.Events;
// The type `CloudEvent` comes directly from the official SDK package// and can be cast implicitly like so, CloudEvent cloudEvent = events.First();
// Or explicitly.CloudEvent cloudEvent = events.First().AsCloudEvent();
#
Deserializing Event Grid EventsWe provide support for deserializing EventGrid events.
using Arcus.EventGrid;using Arcus.EventGrid.Parsers;using Microsoft.Azure.EventGrid.Models;
string eventGridEventJson = ...EventBatch<Event> eventBatch = EventParser.Parse(eventGridEventJson);var events = eventBatch.Events;
// The `EventGridEvent` can be cast implicitly like so, EventGridEvent eventGridEvent = events.First();
// Or explicitly.EventGridEvent eventGridEvent = events.First().AsEventGridEvent();
#
Deserializing Custom EventsWe provide support for deserializing events to typed event objects where the custom event payload is available via the .GetPayload()
method.
If you want to have the original raw JSON event payload, you can get it via the .Data
property.
using Arcus.EventGrid;using Arcus.EventGrid.Parsers;
// Parse from your custom event implementation with the `.Parse<>` function.EventGridBatch<NewCarRegistered> eventGridBatch = EventGridParser.Parse<NewCarRegistered>(rawEvent);
// The event data type will be wrapped inside an `EventGridEvent<>` instance.NewCarRegistered eventGridMessage = eventGridBatch.Events.First();
// The original event payload can now be accessed.CarEventData typedEventPayload = eventGridMessage.GetPayload();object untypedEventPayload = eventGridMessage.Data;