Why can't EventData.GetBytes() be called before sending?

╄→гoц情女王★ 提交于 2019-12-24 02:18:32

问题


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:


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



来源:https://stackoverflow.com/questions/27241679/why-cant-eventdata-getbytes-be-called-before-sending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!