public async Task ObservableEventBatchIsPublishable() { await using var scope = await EventHubScope.CreateAsync(1); await using var producer = new EventHubProducerClient(EventHubsTestEnvironment.Instance.EventHubsConnectionString, scope.EventHubName); using var eventBatch = await producer.CreateBatchAsync(); var observableBatch = new ObservableEventDataBatch(eventBatch); // Adding events to the batch for (var index = 0; index < 5; ++index) { var eventData = new EventData($"Event #{ index }"); eventData.Properties.Add("ApplicationId", index); if (!observableBatch.TryAdd(eventData)) { throw new Exception($"The event at { index } could not be added."); } } var contains = observableBatch.Events.Any(eventData => int.TryParse(eventData.Properties["ApplicationId"].ToString(), out var id) && id == 1); Assert.That(contains, Is.True, "The batch should contain the event with the expected application identifier."); Assert.That(observableBatch.Count, Is.GreaterThan(0), "Events were not successfully added to the batch"); Assert.That(observableBatch.Count, Is.EqualTo(observableBatch.Events.Count), "The observable batch events are out of sync with the event batch data"); // Check implicit casting by verifying batch can be sent using built in // producer method await producer.SendAsync(observableBatch); }
public async Task Sample09_CheckingBatch() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:Sample09_CheckingBatch #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); try { using var eventBatch = await producer.CreateBatchAsync(); var observableBatch = new ObservableEventDataBatch(eventBatch); // Attempt to add events to the batch. for (var index = 0; index < 5; ++index) { var eventData = new EventData($"Event #{ index }") { MessageId = index.ToString() }; if (!observableBatch.TryAdd(eventData)) { throw new Exception($"The event at { index } could not be added."); } } // The "Events" collection can be used to validate that a specific event // is in the batch. In this example, we'll ensure that an event with // id "1" was added. var contains = observableBatch.Events .Any(eventData => eventData.MessageId == "1"); } finally { await producer.CloseAsync(); } #endregion }
public async Task Sample09_AccessingEventData() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:Sample09_AccessingEventData #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); try { using var eventBatch = await producer.CreateBatchAsync(); var observableBatch = new ObservableEventDataBatch(eventBatch); // Attempt to add events to the batch. for (var index = 0; index < 5; ++index) { var eventData = new EventData($"Event #{ index }"); if (!observableBatch.TryAdd(eventData)) { throw new Exception($"The event at { index } could not be added."); } } // Events in the batch can be inspected using the "Events" collection. foreach (var singleEvent in observableBatch.Events) { Debug.WriteLine($"Added event { singleEvent.EventBody } at time { singleEvent.EnqueuedTime }"); } await producer.SendAsync(observableBatch); } finally { await producer.CloseAsync(); } #endregion }
public async Task Sample09_CheckingBatch() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:Sample09_CheckingBatch #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); try { using var eventBatch = await producer.CreateBatchAsync(); var newBatch = new ObservableEventDataBatch(eventBatch); // Adding events to the batch for (var index = 0; index < 5; ++index) { var eventBody = new BinaryData($"Event #{ index }"); var eventData = new EventData(eventBody); eventData.Properties.Add("ApplicationId", index); if (!newBatch.TryAdd(eventData)) { throw new Exception($"The event at { index } could not be added."); } } // Verify that the expected event is in the batch var contains = newBatch.Events.Any(eventData => int.TryParse(eventData.Properties["ApplicationId"].ToString(), out var id) && id == 1); } finally { await producer.CloseAsync(); } #endregion }