Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
È possibile usare le librerie di gestione di Hub eventi per effettuare il provisioning dinamico di entità e spazi dei nomi di Hub eventi. per consentire distribuzioni complesse e scenari di messaggistica e permettere di determinare a livello di codice le entità di cui effettuare il provisioning. Queste librerie sono attualmente disponibili per .NET.
Funzionalità supportate
- Creazione, aggiornamento, eliminazione di spazi dei nomi
- Creazione, aggiornamento, eliminazione di Hub eventi
- Creazione, aggiornamento, eliminazione di gruppi di consumer
Prerequisiti
Per iniziare a usare le librerie di gestione di Hub eventi, è necessario eseguire l'autenticazione con Microsoft Entra ID. Microsoft Entra ID richiede l'autenticazione come entità servizio, che fornisce l'accesso alle risorse di Azure. Per informazioni su come creare un'entità servizio, vedere uno di questi articoli:
- Usare il portale di Azure per creare un'applicazione Active Directory e un'entità servizio che accedono alle risorse
- Usare Azure PowerShell per creare un'entità servizio per accedere alle risorse
- Usare l'interfaccia della riga di comando di Azure per creare un'entità servizio per accedere alle risorse
Nel corso di queste esercitazioni vengono forniti un AppId
(ID client), un TenantId
e un ClientSecret
(chiave di autenticazione) che sono usati per l'autenticazione da parte delle librerie di gestione. L'applicazione Microsoft Entra deve essere aggiunta al ruolo proprietario dati Hub eventi di Azure a livello di gruppo di risorse.
Codice di esempio
Il modello di modifica delle risorse di Hub eventi segue un protocollo comune:
- Ottenere un token dall'ID Microsoft Entra usando la
Microsoft.Identity.Client
libreria. - Creare l'oggetto
EventHubManagementClient
. - Usare quindi l'oggetto client per creare uno spazio dei nomi di Hub eventi e un hub eventi.
Ecco il codice di esempio per creare uno spazio dei nomi di Hub eventi e un hub eventi.
namespace event_hub_dotnet_management
{
using System;
using System.Threading.Tasks;
using Microsoft.Azure.ResourceManager.EventHubs;
using Microsoft.Azure.ResourceManager.EventHubs.Models;
using Microsoft.Identity.Client;
using Microsoft.Rest;
public static class EventHubManagementSample
{
private static string resourceGroupName = "<YOUR EXISTING RESOURCE GROUP NAME>";
private static string namespaceName = "<EVENT HUBS NAMESPACE TO BE CREATED>";
private const string eventHubName = "<EVENT HUB TO BE CREATED>";
private const string location = "<REGION>"; //for example: "eastus"
public static async Task Main()
{
// get a token from Azure AD
var token = await GetToken();
// create an EventHubManagementClient
var creds = new TokenCredentials(token);
var ehClient = new EventHubManagementClient(creds)
{
SubscriptionId = "<AZURE SUBSCRIPTION ID>"
};
// create an Event Hubs namespace using the EventHubManagementClient
await CreateNamespace(ehClient);
// create an event hub using the EventHubManagementClient
await CreateEventHub(ehClient);
Console.WriteLine("Press a key to exit.");
Console.ReadLine();
}
// Get an authentication token from Azure AD first
private static async Task<string> GetToken()
{
try
{
Console.WriteLine("Acquiring token...");
var tenantId = "<AZURE TENANT ID>";
// use the Azure AD app that's a member of Azure Event Hubs Data Owner role at the resource group level
var clientId = "<AZURE APPLICATION'S CLIENT ID>";
var clientSecret = "<CLIENT SECRET>";
IConfidentialClientApplication app;
app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.Build();
var result = await app.AcquireTokenForClient(new[] { $"https://management.core.windows.net/.default" })
.ExecuteAsync()
.ConfigureAwait(false);
// If the token isn't a valid string, throw an error.
if (string.IsNullOrEmpty(result.AccessToken))
{
throw new Exception("Token result is empty!");
}
return result.AccessToken;
}
catch (Exception e)
{
Console.WriteLine("Could not get a new token...");
Console.WriteLine(e.Message);
throw e;
}
}
// Create an Event Hubs namespace
private static async Task CreateNamespace(EventHubManagementClient ehClient)
{
try
{
Console.WriteLine("Creating namespace...");
await ehClient.Namespaces.CreateOrUpdateAsync(resourceGroupName, namespaceName, new EHNamespace { Location = location });
Console.WriteLine("Created namespace successfully.");
}
catch (Exception e)
{
Console.WriteLine("Could not create a namespace...");
Console.WriteLine(e.Message);
}
}
// Create an event hub
private static async Task CreateEventHub(EventHubManagementClient ehClient)
{
try
{
Console.WriteLine("Creating Event Hub...");
await ehClient.EventHubs.CreateOrUpdateAsync(resourceGroupName, namespaceName, eventHubName, new Eventhub());
Console.WriteLine("Created Event Hub successfully.");
}
catch (Exception e)
{
Console.WriteLine("Could not create an Event Hub...");
Console.WriteLine(e.Message);
}
}
}
}