public void NonGenericReceiveAndDeserializeEventGridEvents() { #region Snippet:EGEventParseJson // Parse the JSON payload into a list of events using EventGridEvent.Parse EventGridEvent[] egEvents = EventGridEvent.Parse(jsonPayloadSampleOne); #endregion // Iterate over each event to access event properties and data #region Snippet:DeserializePayloadUsingNonGenericGetData foreach (EventGridEvent egEvent in egEvents) { // If the event is a system event, GetData() should return the correct system event type switch (egEvent.GetData()) { case SubscriptionValidationEventData subscriptionValidated: Console.WriteLine(subscriptionValidated.ValidationCode); break; case StorageBlobCreatedEventData blobCreated: Console.WriteLine(blobCreated.BlobType); break; case BinaryData unknownType: // An unrecognized event type - GetData() returns BinaryData with the serialized JSON payload if (egEvent.EventType == "MyApp.Models.CustomEventType") { // You can use BinaryData methods to deserialize the payload TestPayload deserializedEventData = unknownType.ToObjectFromJson <TestPayload>(); Console.WriteLine(deserializedEventData.Name); } break; } } #endregion }
public void NonGenericReceiveAndDeserializeEventGridEvents() { #region Snippet:EGEventParseJson // Parse the JSON payload into a list of events using EventGridEvent.Parse EventGridEvent[] egEvents = EventGridEvent.Parse(jsonPayloadSampleOne); #endregion // Iterate over each event to access event properties and data #region Snippet:DeserializePayloadUsingAsSystemEventData foreach (EventGridEvent egEvent in egEvents) { // If the event is a system event, TryGetSystemEventData() will return the deserialized system event if (egEvent.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(egEvent.EventType); // we can get the raw Json for the event using GetData() Console.WriteLine(egEvent.GetData().ToString()); break; } } else { switch (egEvent.EventType) { case "MyApp.Models.CustomEventType": TestPayload deserializedEventData = egEvent.GetData <TestPayload>(); Console.WriteLine(deserializedEventData.Name); break; // Handle any other custom event type default: Console.Write(egEvent.EventType); Console.WriteLine(egEvent.GetData().ToString()); break; } } } #endregion }
public static async System.Threading.Tasks.Task Run( [EventGridTrigger] string eventGridEventAsString, ExecutionContext context, ILogger log) { if (string.IsNullOrWhiteSpace(eventGridEventAsString)) { log.LogError("Received a null or empty event."); return; } var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddUserSecrets(Assembly.GetExecutingAssembly()) // application setting on local box .AddEnvironmentVariables() // application settings from Azure .Build(); var appConfiguration = config.Get <AppConfiguration>() ?? throw new ArgumentNullException("Configuration is required"); appConfiguration.Validate(); var httpContent = new BinaryData(eventGridEventAsString).ToStream(); var eventGridEvent = EventGridEvent.Parse(BinaryData.FromStream(httpContent)); if (eventGridEvent.TryGetSystemEventData(out object systemEvent)) { switch (systemEvent) { case ContainerRegistryImagePushedEventData pushEvent: log.LogInformation($"Received a push event for artifact '{pushEvent.Target.Repository}:{pushEvent.Target.Tag}' of registry '{pushEvent.Request.Host}'"); await ImportImageAsync(pushEvent, appConfiguration, log); break; case ContainerRegistryImageDeletedEventData deletedEvent: log.LogInformation($"Received a delete event for artifact '{deletedEvent.Target.Repository}:{deletedEvent.Target.Digest}' of registry '{deletedEvent.Request.Host}'"); await DeleteImageAsync(deletedEvent, appConfiguration, log); break; default: log.LogWarning($"Received an unexpected ACR event data. Expected a push/delete event. Received '{eventGridEvent.EventType}'"); break; } } else { log.LogError("Could not parse the event to ACR Event schema"); } }
public void HandleEventGridNotification(string data) { var events = EventGridEvent.Parse(data); foreach (EventGridEvent eventGridEvent in events) { if (eventGridEvent.TryGetSystemEventData(out object systemData) && systemData is AppConfigurationKeyValueModifiedEventData valueModifiedEventData) { // TODO: Uncomment when EventGrid is updated with a definition that includes SyncToken // SharedConfigurationClient.AddSyncToken(valueModifiedEventData.SyncToken); Response <ConfigurationSetting> updatedSetting = SharedConfigurationClient.GetConfigurationSetting(valueModifiedEventData.Key, valueModifiedEventData.Label); Console.WriteLine($"Setting was updated. Key: {updatedSetting.Value.Key} Value: {updatedSetting.Value.Value}"); } } }