Azure Event Grid-Bibliotheken für .NETAzure Event Grid libraries for .NET

Buildereignisgesteuerte Anwendungen, die mit einfacher HTTP-basierter Ereignisbehandlung und Azure Event Grid auf Ereignisse von Azure-Diensten und benutzerdefinierten Quellen lauschen und reagierenBuild event-driven applications that listen and react to events from Azure services and custom sources using simple HTTP-based event handling with Azure Event Grid.

Weitere Informationen zu Azure Event Grid und erste Schritte mit dem Tutorial zu Azure Blob Storage-EreignissenLearn more about Azure Event Grid and get started with the Azure Blob storage event tutorial.

Client-SDKClient SDK

Mit dem Client-SDK von Azure Event Grid können Sie Ereignisse erstellen, die Authentifizierung durchführen und Beiträge in Themen posten.Create events, authenticate, and post to topics using the Azure Event Grid Client SDK.

Installieren Sie das NuGet-Paket direkt über die Paket-Manager-Konsole in Visual Studio oder mit der .NET Core CLI.Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio-Paket-ManagerVisual Studio Package Manager

Install-Package Microsoft.Azure.EventGrid

.NET Core CLI.NET Core CLI

dotnet add package Microsoft.Azure.EventGrid 

Veröffentlichen von EreignissenPublish events

Der folgende Code authentifiziert sich mit Azure und veröffentlicht List von EventGridEvent-Ereignissen eines benutzerdefinierten Typs (in diesem Beispiel Contoso.Items.ItemsReceivedEvent) in einem Thema.The following code authenticates with Azure and publishes a List of EventGridEvent events of a custom type (in this example, Contoso.Items.ItemsReceivedEvent ) to a topic. Der im Beispiel verwendete Schlüssel und die Endpunktadresse des Themas können über Azure PowerShell abgerufen werden:The topic key and endpoint address used in the sample can be retrieved from Azure PowerShell:

$endpoint = (Get-AzureRmEventGridTopic -ResourceGroupName gridResourceGroup -Name <topic-name>).Endpoint
$keys = Get-AzureRmEventGridTopicKey -ResourceGroupName gridResourceGroup -Name <topic-name>
string topicEndpoint = "https://<topic-name>.<region>-1.eventgrid.azure.net/api/events";
string topicKey = "<topic-key>";
string topicHostname = new Uri(topicEndpoint).Host;

TopicCredentials topicCredentials = new TopicCredentials(topicKey);
EventGridClient client = new EventGridClient(topicCredentials);

client.PublishEventsAsync(topicHostname, GetEventsList()).GetAwaiter().GetResult();
Console.Write("Published events to Event Grid.");

static IList<EventGridEvent> GetEventsList()
{
    List<EventGridEvent> eventsList = new List<EventGridEvent>();
    for (int i = 0; i < 1; i++)
    {
        eventsList.Add(new EventGridEvent()
        {
            Id = Guid.NewGuid().ToString(),
            EventType = "Contoso.Items.ItemReceivedEvent",
            Data = new ContosoItemReceivedEventData()
            {
                ItemUri = "ContosoSuperItemUri"
            },

            EventTime = DateTime.Now,
            Subject = "Door1",
            DataVersion = "2.0"
        });
    }
    return eventsList;
}

Nutzen von EreignissenConsume events

In diesem Codeausschnitt werden Ereignisse genutzt, z.B. das benutzerdefinierte Ereignis Contoso.Items.ItemsReceived sowie Ereignisse, die über andere Azure-Dienste, z.B. Blob Storage, ausgelöst werden.This snippet consumes events, including a custom event Contoso.Items.ItemsReceived as well as events triggered from other Azure services, such as Blob Storage.

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;
    }
}

Verwaltungs-SDKManagement SDK

Das Verwaltungs-SDK ermöglicht Ihnen das Erstellen, Aktualisieren und Löschen von Event Grid-Instanzen, -Themen und -Abonnements.Create, update, or delete Event Grid instances, topics, and subscriptions with the management SDK.

Installieren Sie das NuGet-Paket direkt über die Paket-Manager-Konsole in Visual Studio oder mit der .NET Core CLI.Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio-Paket-ManagerVisual Studio Package Manager

Install-Package Microsoft.Azure.Management.EventGrid

.NET Core CLI.NET Core CLI

dotnet add package Microsoft.Azure.Management.EventGrid

Weitere InformationenLearn more