【问题标题】:Usage of 'using' causes ObjectDisposedException in creating list of EventData在创建 EventData 列表时使用“使用”会导致 ObjectDisposedException
【发布时间】:2016-01-14 07:24:58
【问题描述】:

我通常如下向事件中心发送数据..

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'

在访问 throws 时..

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

真诚感谢任何帮助..

谢谢

【问题讨论】:

  • 见丹尼陈的回答。 using 块用于在不再需要对象时自动处理它们。如果您需要 using 块之外的对象,请不要使用。

标签: c# azure azure-eventhub


【解决方案1】:

因为在第一个代码 sn-p 中,您在 using 块内发送 edata。但是在第二个 sn-p 中,您将 edata 放入一个列表中,然后循环遍历列表并在 using 块之后发送每个项目,其中项目 edata 已经被释放。

【讨论】:

    猜你喜欢
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多