"EG" here refers to Azure Event Grid. Passing data with Event Grid involves three pieces of configuration: the topic (where events are published), the event subscription (who receives which events), and the event payload (the data itself). 1. Create a custom topic (or use a system topic) - Custom topic: your application publishes its own events, for example "Order.Created". - System topics: Azure services publish events automatically (Blob Storage "BlobCreated", Resource Groups, Key Vault). - Choose the schema: Event Grid schema or CloudEvents v1.0 (recommended for interoperability). 2. Publish an event with data (C#, Azure.Messaging.EventGrid) var client = new EventGridPublisherClient(new Uri(topicEndpoint), new DefaultAzureCredential()); var evt = new CloudEvent( source: "/orders-api", type: "Shop.Order.Created", jsonSerializableData: new { OrderId = 1234, CustomerId = "C-77", Total = 2499.00m }) { Subject = "orders/1234" }; await client.SendEventAsync(evt); The publishing identity needs the EventGrid Data Sender role. 3. Configure an event subscription - Endpoint types: Azure Function, Service Bus queue or topic, Storage queue, Event Hubs, or a webhook (HTTPS). - Filters so subscribers receive only relevant events: - Event types: includedEventTypes ["Shop.Order.Created"] - Subject filters: subjectBeginsWith "orders/" and subjectEndsWith - Advanced filters on data fields: data.Total greater than 1000 - Retry policy: maximum delivery attempts and event time-to-live (defaults are 30 attempts and 24 hours). - Dead-lettering to a storage container for events that cannot be delivered. - Delivery identity (managed identity) for secure delivery to Service Bus or Event Hubs. Example (Azure CLI): az eventgrid event-subscription create --name order-created-to-fn \ --source-resource-id <topic-resource-id> \ --endpoint-type azurefunction --endpoint <function-resource-id> \ --included-event-types Shop.Order.Created --subject-begins-with orders/ \ --max-delivery-attempts 10 --event-ttl 1440 \ --deadletter-endpoint <storage-account-id>/blobServices/default/containers/deadletters 4. Consume the data (Azure Function) [Function("OnOrderCreated")] public void Run([EventGridTrigger] CloudEvent evt) => _logger.LogInformation("Order {Id}", evt.Data!.ToObjectFromJson<OrderCreated>()!.OrderId); Design guidance - Keep event payloads small (Event Grid events are limited to 1 MB); include IDs and key fields, and let consumers fetch details if needed (the claim-check pattern for large data). - Make consumers idempotent; delivery is at least once. - Version event types (Shop.Order.Created.v2) when the schema changes.
Ravindra Wadile · · 0 likes