in Sample-Code-Snippets/dotnet/EventHubs-Emulator-Demo/EventHubs-Emulator-Demo/Program.cs [30:59]
private static async Task Send()
{
// number of events to be sent to the event hub
int numOfEvents = 100;
EventHubProducerClient producerClient = new EventHubProducerClient(eventHubNamespaceConnectionString, eventHubName);
// Create a batch of events
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
for (int i = 1; i <= numOfEvents; i++)
{
if (!eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($"Event {i}"))))
{
// if it is too large for the batch
throw new Exception($"Event {i} is too large for the batch and cannot be sent.");
}
}
try
{
// Use the producer client to send the batch of events to the event hub
await producerClient.SendAsync(eventBatch);
Console.WriteLine($"A batch of {numOfEvents} events has been published.");
}
finally
{
await producerClient.DisposeAsync();
}
}