C# 使用';使用';在创建事件数据列表时导致ObjectDisposedException
我通常将数据发送到事件中心,如下所示C# 使用';使用';在创建事件数据列表时导致ObjectDisposedException,c#,azure,azure-eventhub,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).Con
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);
}
在访问时
'eventData.SerializedSizeInBytes' threw an exception of type 'System.ObjectDisposedException'
衷心感谢您的帮助
谢谢,因为在第一个代码片段中,您使用块在
内部发送edata
。但是在第二个代码段中,您将edata
放在一个列表中,然后在列表中循环,并在using
块之后发送每个项目,其中项目edata
已经被处理。因为在第一个代码段中,您在using
块内部发送edata
。但是在第二个代码片段中,您将edata
放入一个列表中,然后在列表中循环,并使用块在之后发送每个项目,其中项目edata
已被处理。参见Danny Chen的答案<代码>使用
块用于在不再需要对象时自动处理对象。如果您需要使用块以外的对象,请不要使用。请参阅Danny Chen的答案<代码>使用
块用于在不再需要对象时自动处理对象。如果您需要使用块以外的对象,请不要使用。
'eventData.SerializedSizeInBytes' threw an exception of type 'System.ObjectDisposedException'