public void PublishEventsToTopic() { using (MockContext context = MockContext.Start(this.GetType().FullName)) { this.InitializeClients(context); var location = this.ResourceManagementClient.GetLocationFromProvider(); var resourceGroup = this.ResourceManagementClient.TryGetResourceGroup(location); if (string.IsNullOrWhiteSpace(resourceGroup)) { resourceGroup = TestUtilities.GenerateName(EventGridManagementHelper.ResourceGroupPrefix); this.ResourceManagementClient.TryRegisterResourceGroup(location, resourceGroup); } var topicName = TestUtilities.GenerateName(EventGridManagementHelper.TopicPrefix); var originalTagsDictionary = new Dictionary <string, string>() { { "originalTag1", "originalValue1" }, { "originalTag2", "originalValue2" } }; Topic topic = new Topic() { Location = location, Tags = originalTagsDictionary }; var createTopicResponse = this.EventGridManagementClient.Topics.CreateOrUpdate(resourceGroup, topicName, topic); Assert.NotNull(createTopicResponse); Assert.Equal(createTopicResponse.Name, topicName); TestUtilities.Wait(TimeSpan.FromSeconds(60)); // Get the topic key TopicSharedAccessKeys keys = this.EventGridManagementClient.Topics.ListSharedAccessKeys(resourceGroup, topicName); // Publish events to topic string topicHostname = new Uri(createTopicResponse.Endpoint).Host; TopicCredentials topicCredentials = new TopicCredentials(keys.Key1); EventGridClient client = EventGridManagementHelper.GetEventGridClient( context, topicCredentials, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }); Console.WriteLine("Publishing to Azure Event Grid"); client.PublishEventsAsync(topicHostname, GetEventsList()).GetAwaiter().GetResult(); Console.WriteLine("Published successfully!"); // Delete topic this.EventGridManagementClient.Topics.Delete(resourceGroup, topicName); } }
static async Task PerformTopicAndEventSubscriptionOperations() { string token = await GetAuthorizationHeaderAsync(); TokenCredentials credential = new TokenCredentials(token); ResourceManagementClient resourcesClient = new ResourceManagementClient(credential) { SubscriptionId = SubscriptionId }; EventGridManagementClient eventGridManagementClient = new EventGridManagementClient(credential) { SubscriptionId = SubscriptionId, LongRunningOperationRetryTimeout = 2 }; try { // Register the EventGrid Resource Provider with the Subscription await RegisterEventGridResourceProviderAsync(resourcesClient); // Create a new resource group await CreateResourceGroupAsync(ResourceGroupName, resourcesClient); // Create a new Event Grid topic in a resource group await CreateEventGridTopicAsync(ResourceGroupName, TopicName, eventGridManagementClient); // Get the keys for the topic TopicSharedAccessKeys topicKeys = await eventGridManagementClient.Topics.ListSharedAccessKeysAsync(ResourceGroupName, TopicName); Console.WriteLine($"The key1 value of topic {TopicName} is: {topicKeys.Key1}"); // Create an event subscription await CreateEventGridEventSubscriptionAsync(ResourceGroupName, TopicName, EventSubscriptionName, eventGridManagementClient, EndpointUrl); // Delete the event subscription await DeleteEventGridEventSubscriptionAsync(ResourceGroupName, TopicName, EventSubscriptionName, eventGridManagementClient); // Delete an EventGrid topic with the given topic name and a resource group await DeleteEventGridTopicAsync(ResourceGroupName, TopicName, eventGridManagementClient); Console.WriteLine("Press any key to exit..."); Console.ReadLine(); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadLine(); } }