I am working with the EventHubBufferedProducerClient class of the Azure SDK for .NET (Azure.Messaging.EventHubs v. 5.7.5); I need to send two groups of messages, with the second group starting after publishing the first. No issues with the first group: I enqueue them and then use the FlushAsync method to make sure all the messages in the buffer are sent for publication. When I try to enqueue a message of the second group, though, I receive an ObjectDisposedException: 'The CancellationTokenSource has been disposed.'.
NB: I do not use the EventHubProducerClient because I need to tailor the Partition Key to each message.
I also tried the following "toy code" (I hid the actual connstring and hubname for posting) , to be sure the issue is not related to the processing of the data before and after publication - the issue also repeats with this code.
static async Task Main(string[] args)
{
EventHubBufferedProducerClient client = new EventHubBufferedProducerClient("connectionstring", "eventhubname");
client.SendEventBatchFailedAsync += args =>
{
return Task.CompletedTask;
};
for (int i = 0; i<3; i++)
{
EventData data = new EventData($"string {i}");
await client.EnqueueEventAsync(data);
}
await client.FlushAsync();
for (int i = 3; i < 6; i++)
{
EventData data = new EventData($"string {i}");
await client.EnqueueEventAsync(data); //EXCEPTION HERE at the first iteration
}
await client.FlushAsync();
}
I know I can "solve" this by creating a new instance of the client to enqueue and publish the second group of events, but I'm not sure it's the best solution; I'm also quite curious to understand why the issue happens.
Edit
After the question has been closed: I'm not sure why it was marked as "opinion-based". I would like to know why the "FlushAsync" method is not working how it is supposed to work, I should be able to enqueue messages after flushing, but I cannot. What am I doing wrong (both with code and question, it seems)?