我正在使用Microsoft.Azure.EventHubs 2.0.0 NuGet将消息添加到批处理中:
private EventDataBatch _currentBatch;
_currentBatch = _eventHubClient.CreateBatch();
...
var json = @event.ToEventJson();
var data = new EventData(Encoding.UTF8.GetBytes(json));
if (_currentBatch.TryAdd(data))
{
return;
}
但是创建的批次随机(不是在所有创建的批次上)抛出:
{Microsoft.Azure.EventHubs.MessageSizeExceededException:已收到 消息(传递ID:0,大小:262192字节)超出了限制(262144) 链接上当前允许的字节数。
在
await _eventHubClient.SendBatchAsync(batch);
这是确定性的,因为我有一组数据总是在测试期间抛出此错误。
答案 0 :(得分:0)
Update:
If you're using Microsoft.Azure.EventHubs 2.0.0 NuGet
, and the message size is larger than 256kb, you should see this error message:
System.ArgumentException: 'There are no event data supplied. Please make sure input events are not empty.
Parameter name: eventDatas'.
if you didn't explicitly set the max size(it can be as long as 1M), then the default max size is 256kb.
The method _currentBatch.TryAdd(data
) will check the size if it exceeds the max size you set. If it exceeds the max size, then return false, and if it returns false, then the SendAsync()
method will throw an exception; else return true.
You can explicitly set the max size(it can be as long as 1M) using following code:
BatchOptions bo = new BatchOptions() { MaxMessageSize = 1048576 };
_currentBatch = eventHubClient.CreateBatch(bo);
The sample code as blow, the data to send is larger than 256kb and less than 1M:
private static async Task SendTest()
{
var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
{
EntityPath = EventHubName
};
eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
EventDataBatch _currentBatch;
BatchOptions bo = new BatchOptions() { MaxMessageSize = 1048576 }; // the max size can be 1M
_currentBatch = eventHubClient.CreateBatch(bo);
StreamReader sr = new StreamReader(@"D:\azure stack overflow\2019\04\18\test1.txt");
string s = sr.ReadToEnd();
Console.WriteLine(s.Length);
var data = new EventData(Encoding.UTF8.GetBytes(s));
if (_currentBatch.TryAdd(data))
{
Console.WriteLine("data has been added successfully!!!");
//return;
}
else
{
Console.WriteLine("no data added,the data is too larger!!!");
}
await eventHubClient.SendAsync(_currentBatch);
}
And it works without throw exception:
The exception "MessageSizeExceededException
" is thrown due to this:
Thrown if the total serialized size of eventDataList exceeds the allowed size limit for one event transmission (256k by default).
Please make sure the size no more than 256k.
You can refer to the document here for more details.