本文介绍了为什么在发送之前无法调用EventData.GetBytes()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure事件中心,最初在发送数据以尝试计算批处理大小时,我具有类似于以下代码的代码,该代码将调用 EventData . GetBytes

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);

不幸的是,我会在SDK代码中收到一个异常.

Unfortunately I would receive an exception in the SDK code.

尽管删除了对GetBytes的最终不必要的调用,这意味着我可以发送消息,但是发生此异常的原因令人费解.连续两次调用GetBytes()是重现相同异常的一种简便方法,但是一次调用将意味着EventData无法成功发送.

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.

消息,并且设置为如果 Message.GetBody 文档;但是,EventData的方法中没有达到此目的的文档 GetBodyStream 带有串行化器的GetBody GetBody GetBytes .

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.

推荐答案

您是否尝试过使用EventData.SerializedSizeInBytes来获取大小?这是获取用于批处理计算的大小的更准确的方法.

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

这篇关于为什么在发送之前无法调用EventData.GetBytes()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 12:21