Why can't EventData.GetBytes() be called befor

2019-07-19 01:44发布

I'm working with Azure Event Hubs and initially when sending data to try and calculate batch size I had code similar to the below that would call EventData.GetBytes

EventHubClient client;//initialized before the relevant code

EventData curr = new EventData(data);
//Setting a partition key, and other operations.
long itemLength = curr.GetBytes().LongLength;
client.SendAsync(curr);

Unfortunately I would receive an exception in the SDK code.

The message body cannot be read multiple times. To reuse it store the value after reading.

While removing the ultimately unnecessary call to GetBytes meant that I could send messages, the rationale for this exception to occur is rather puzzling. Calling GetBytes() twice in a row is an easy way to reproduce the same exception, but a single call will mean that the EventData cannot be sent successfully.

It seems likely that underneath a Message is used and this is set to throw an exception if called more than once as Message.GetBody documents; however, there is no documentation to this effect in EventData's methods GetBodyStream, GetBody w/serializer, GetBody, or GetBytes.

I imagine this should either be documented, or corrected since currently it is an unpleasant surprise in a separate thread.

1条回答
地球回转人心会变
2楼-- · 2019-07-19 02:32

Have you tried using EventData.SerializedSizeInBytes to get the size? that is a much more accurate way to get the size for batching calculation.

查看更多
登录 后发表回答