コード例 #1
0
        /// <summary>
        /// Delete all EventHub paths in a given eventhub namespace
        /// </summary>
        /// <param name="eventHubConnectionString">The connection string used to access the EventHub namespace</param>
        public static void DeleteAllEventHubs(string eventHubConnectionString)
        {
            NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(eventHubConnectionString);

            Console.WriteLine("Deleting the EventHub data in EventHub with connection string: '{0}'", eventHubConnectionString);
            foreach (var eventhub in RetryPolicy.ExecuteAsync(namespaceManager.GetEventHubsAsync).Result)
            {
                Console.WriteLine("Deleting EventHub '{0}'", eventhub.Path);
                RetryPolicy.ExecuteAsync(() => namespaceManager.DeleteEventHubAsync(eventhub.Path)).Wait();
            }
        }
コード例 #2
0
        public static async Task Main(string[] args)
        {
            // Create token provider so that we can use it at both management and runtime clients.
            TokenProvider tokenProvider = TokenProvider.CreateAzureActiveDirectoryTokenProvider(
                async(audience, authority, state) =>
            {
                IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(ClientId)
                                                     .WithAuthority(authority)
                                                     .WithClientSecret(ClientSecret)
                                                     .Build();

                var authResult = await app.AcquireTokenForClient(new string[] { $"{audience}/.default" }).ExecuteAsync();
                return(authResult.AccessToken);
            },
                ServiceAudience.EventHubsAudience,
                $"https://login.microsoftonline.com/{TenantId}");

            var eventHubName = "testeh-" + Guid.NewGuid().ToString();

            // Create NamespaceManger and EventHubClient with Azure Active Directory token provider.
            var ehUri            = new Uri($"sb://{EventHubNamespace}/");
            var namespaceManager = new NamespaceManager(ehUri, tokenProvider);
            var messagingFactory = MessagingFactory.Create(ehUri,
                                                           new MessagingFactorySettings()
            {
                TokenProvider = tokenProvider,
                TransportType = TransportType.Amqp
            });
            var ehClient = messagingFactory.CreateEventHubClient(eventHubName);

            // Create a new event hub.
            Console.WriteLine($"Creating event hub {eventHubName}");
            await namespaceManager.CreateEventHubAsync(eventHubName);

            // Send and receive a message.
            await SendReceiveAsync(ehClient);

            // Delete event hub.
            Console.WriteLine($"Deleting event hub {eventHubName}");
            await namespaceManager.DeleteEventHubAsync(eventHubName);

            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }