Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用';使用';在创建事件数据列表时导致ObjectDisposedException_C#_Azure_Azure Eventhub - Fatal编程技术网

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'