In the documentation for the Azure EventHubClient there are two methods for sending a batch of data each of them has the remark below and will throw a MessageSizeExceededException if it is ignored.
You should ensure that the total serialized size of eventDataList is less than the size limit of one event data transmission, which is 256k by default.
A similar warning is present in the Programming Guide
How can the serialized size of IEnumerable<EventData> eventDataList
be determined?
The size of the bytes passed to each EventData is easy enough to determine, assuming you don't ask the EventData. However, the serialized form of an EventData presumably includes the Partition Key and user properties as used in the sample.
data.Properties.Add("Type","Telemetry_" + DateTime.Now.ToLongTimeString());
Currently my only option looks like being conservative with batch sizing.
----Updating after the Nuget Update---- Here's the EventData property - SerializedSizeInBytes that we introduced in our recent SDK iteration - which addresses this specific problem - available in all sdk nugets above 2.6 .
---Update after
EventDataBatch
utility---use
EventDataBatch.TryAdd(EventData)
API to construct the batch. When theEventData
cannot fit into the batch - this API will returnfalse
. Stop adding more events & then use the sender (EventHubClient
orPartitionSender
) to send theEventDataBatch
.following considerations motivated us to design
EventDataBatch
API:EventData
MaximumMessageSize
configuration:EventHubs
service across the world could have differentMax EventData
sizes - across differentEventHub
regions and differentSKU
s ofEventHub
. We wanted to expose a uniform way for developers to constructmaximum message size
for any given environment. So, whenEventDataBatch
is initialized usingeventHubClient.createBatch()
API - the client negotiatesbatchSize
withEventHubs Service
and returns theEventDataBatch
instance which can construct the correct size!HTH! Sree