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
Endpointuses the Azure EventGrid SDK to access the schema of the CloudEvents message. - The
Endpointenables CloudEvents support and configures the type mapping. - The
Endpointconfigures the serializer to support fields and properties with different casing. - The
Endpointreceives 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
- Run the sample.
- Generate the
BlobCreatedevent by creating a new file in the Azure Blob Storage container. - Observe that the sample prints out the URL of the newly created file.