问题描述
我正在使用EventGrid库,并且需要获得订阅和之前发送的消息。该示例的链接是这样的:https://docs.microsoft.com/en-us/dotnet/api/overview/azure/eventgrid?view=azure-dotnet
代码是:
string response = string.Empty;
string requestContent = await req.Content.ReadAsStringAsync();
EventGridSubscriber eventGridSubscriber = new EventGridSubscriber();
// Optionally add one or more custom event type mappings
eventGridSubscriber.AddOrUpdateCustomEventMapping("Contoso.Items.ItemReceived",typeof(ContosoItemReceivedEventData));
var events = eventGridSubscriber.DeserializeEventGridEvents(requestContent);
foreach (EventGridEvent receivedEvent in events)
{
if (receivedEvent.Data is SubscriptionValidationEventData)
{
SubscriptionValidationEventData eventData = (SubscriptionValidationEventData)receivedEvent.Data;
log.Info($"Got SubscriptionValidation event data,validationCode: {eventData.ValidationCode},validationUrl: {eventData.ValidationUrl},topic: {eventGridEvent.Topic}");
// Handle subscription validation
}
else if (receivedEvent.Data is StorageBlobCreatedEventData)
{
StorageBlobCreatedEventData eventData = (StorageBlobCreatedEventData)receivedEvent.Data;
log.Info($"Got BlobCreated event data,blob URI {eventData.Url}");
// Handle StorageBlobCreatedEventData
}
else if (receivedEvent.Data is ContosoItemReceivedEventData)
{
ContosoItemReceivedEventData eventData = (ContosoItemReceivedEventData)receivedEvent.Data;
}
}
解决方法
您可以阅读有关通过事件网格发布和使用事件的本教程:Microsoft Azure Event Grid Publish/Consume Samples for C#
它还为您提供了不同的样本来达到相同的目的。
让我知道您是否遇到任何问题。