Skip to main content
Version: v3.0.0

Publishing Events

Publishing Events#

We provide support for publishing custom events to a custom Azure Event Grid Topics.

Import the following namespace into your project:

using Arcus.EventGrid.Publishing;

Next, create an EventGridPublisher instance via the EventGridPublisherBuilder which requires the endpoint & authentication key of your custom topic endpoint.

var eventGridPublisher = EventGridPublisherBuilder                                .ForTopic(topicEndpoint)                                .UsingAuthenticationKey(endpointKey)                                .Build();

Publishing EventGridEvent's

Create your event that you want to publish

string licensePlate = "1-TOM-337";string eventSubject = $"/cars/{licensePlate}";string eventId = Guid.NewGuid().ToString();var @event = new NewCarRegistered(eventId, eventSubject, licensePlate);
await eventGridPublisher.PublishAsync(@event);

Alternatively you can publish a list of events by using

await eventGridPublisher.PublishManyAsync(events);

Publishing CloudEvent's

Create your event that you want to publish

string licensePlate = "1-TOM-337";string eventSubject = $"/cars/{licensePlate}";string eventId = Guid.NewGuid().ToString(); var @event = new CloudEvent(    CloudEventsSpecVersion.V1_0,     "NewCarRegistered",     new Uri("https://eventgrid.arcus-azure.net/"),     eventSubject,     eventId){    Data = new CarEventData(licensePlate),    DataContentType = new ContentType("application/json")};
await eventGridPublisher.PublishAsync(@event);

Alternatively you can publish a list of events using

await eventGridPublisher.PublishManyAsync(events);

Publishing Raw Events#

Publishing raw EventGridEvent's

We provide the capability to push EventGridEvents without a schema based on a raw JSON string.

// Created via EventGridPublisherBuilder.EventGridPublisher eventGridPublisher = ...
string licensePlate = "1-TOM-337";string eventSubject = $"/cars/{licensePlate}";string eventId = Guid.NewGuid().ToString();string rawEventPayload = String.Format("{ \"licensePlate\": \"{0}\"}", licensePlate);
await eventGridPublisher.PublishRawEventGridEventAsync(eventId, eventSubject, rawEventPayload);

Publishing raw CloudEvent's

We provide the capability to push CloudEvent's without a schema based on a raw JSON string.

EventGridPublisher eventGridPublisher = ...
string licensePlate = "1-TOM-337";string eventSubject = $"/cars/{licensePlate}";string eventId = Guid.NewGuid().ToString();string rawEventPayload = String.Format("{ \"licensePlate\": \"{0}\"}", licensePlate);
await eventGridPublisher.PublishRawCloudEventAsync(eventId, eventSubject, rawEventPayload);

Resilient Publishing#

The EventGridPublisherBuilder also provides several ways to publish events in a resilient manner. Resilient meaning we support three ways to add resilience to your event publishing:

  • Exponential retry: makes the publishing resilient by retrying a specified number of times with exponential back off.
EventGridPublisherBuilder.ForTopic(topicEndpoint)                         .UsingAuthenticationKey(endpointKey)                         .WithExponentialRetry(retryCount)                         .Build();
  • Circuit breaker: makes the publishing resilient by breaking the circuit if the maximum specified number of exceptions are handled by the policy. The circuit will stay broken for a specified duration. Any attempt to execute the function while the circuit is broken will result in a BrokenCircuitException.
EventGridPublisherBuilder.ForTopic(topicEndpoint)                         .UsingAuthenticationKey(endpointKey)                         .WithCircuitBreaker(exceptionsBeforeBreaking, durationOfBreak)                         .Build();
  • Combination of the two: Circuit breaker with/after exponential retry.
EventGridPublisherBuilder.ForTopic(topicEndpoint)                         .UsingAuthenticationKey(endpointKey)                         .WithExponentialRetry(retryCount)                         .WithCircuitBreaker(exceptionsBeforeBreaking, durationOfBreak)                         .Build();

โ† back