Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Modernization
Samples

Azure Service Bus CloudEvents Sample

Prerequisites

An environment variable named AzureServiceBus_ConnectionString with the connection string for the Azure Service Bus namespace.

Azure Event Grid needs to be configured to receive Azure Blob Storage notifications and deliver them to Azure Service Bus using the CloudEvents schema. The configuration is described in the Microsoft documentation.

Code walk-through

This sample shows an endpoint receiving a CloudEvents message from Azure Service Bus. Such a message can be created by Azure Blob Storage and delivered to Azure Service Bus via Azure Event Grid.

  • The Endpoint uses the Azure EventGrid SDK to access the schema of the CloudEvents message.
  • The Endpoint enables CloudEvents support and configures the type mapping.
  • The Endpoint configures the serializer to support fields and properties with different casing.
  • The Endpoint receives the message and calls the proper handler.

CloudEvents message schema

The message schema is defined in the Azure Event Grid SDK.

This schema matches the Event Grid notifications schema.

CloudEvents support configuration

CloudEvents support must be explicitly enabled:

var cloudEventsConfiguration = endpointConfiguration.EnableCloudEvents();

The configuration includes the type mapping to match the messages with the classes:

cloudEventsConfiguration.TypeMappings["Microsoft.Storage.BlobCreated"] = [typeof(StorageBlobCreatedEventData)];

To support the differences between uppercase letters and lowercase letters in the schema definition and content, the serializer is configured to use case insensitive mapping:

endpointConfiguration.UseSerialization<SystemJsonSerializer>().Options(new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    IncludeFields = true
});

Running the sample

  1. Run the sample.
  2. Generate the BlobCreated event by creating a new file in the Azure Blob Storage container.
  3. Observe that the sample prints out the URL of the newly created file.

Related Articles