c# - 使用 'using' 会导致创建 EventData 列表时出现 ObjectDisposeException

标签 c# azure azure-eventhub

我通常按如下方式将数据发送到事件中心..

var encoded = Encoding.UTF8.GetBytes(serializedString);
using (var edata = new EventData(encoded) { PartitionKey = mypkey })
{
    edata.Properties[EventDataPropertyKeys.MyKey] = myvalue;
    await _eventclient.SendAsync(edata).ConfigureAwait(false);                        
}

今天我想尝试通过批量发送数据,并尝试创建一个 EventData 对象列表,如下所示..

List<EventData> eventDataList = new List<EventData>();

//in a loop
var encoded = Encoding.UTF8.GetBytes(serializedString);    
using (var edata = new EventData(encoded) { PartitionKey = mypkey })
{
    edata.Properties[EventDataPropertyKeys.MyKey] = myvalue;
    eventDataList.Add(edata);
}

但是当我检查 eventdatalist 对象时,我发现 EventData 对象的 SerializedSizeInBytes 属性显示

'This eventdata instance has already been disposed'

并且在访问抛出时..

'eventData.SerializedSizeInBytes' threw an exception of type 'System.ObjectDisposedException'

真诚感谢任何帮助..

谢谢

最佳答案

因为在第一个代码片段中,您在 using block 内发送 edata。但在第二个片段中,您将 edata 放入列表中,然后循环遍历列表并在 using block 之后发送每个项目,其中项目 edata 已被处置。

关于c# - 使用 'using' 会导致创建 EventData 列表时出现 ObjectDisposeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34783792/

相关文章:

c# - EventProcessorClient 事件之间的延迟

c# - DrawUserIndexedPrimitives : Why do the indices have to be short instead of int?

c# - 隐式(默认)值与显式值

azure - 如何测试链接的ARM模板?

c# - 如何使用 ADAL/OpenId 获取 azure AAD 中的当前用户?

node.js - 未收到从 azure-event-hubs onMessage 函数发送的 azure-iothub 消息

c# - 为什么默认的字符串比较器无法保持传递一致性?

c# - StackPanel 逆序 - WPF

Azure 上的 Django 应用程序未获取静态文件

azure - 将 Azure Application Insights 数据发送到事件中心