Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# AggregateException未被捕获?_C#_Exception_Stream - Fatal编程技术网

C# AggregateException未被捕获?

C# AggregateException未被捕获?,c#,exception,stream,C#,Exception,Stream,我正在查询远程服务器,有时会得到一个aggregateeexception。这是相当罕见的,我知道发生这种情况时如何处理,但出于某种原因,每当抛出异常时,它不会进入catch块 这是catch块的代码部分: try { using (Stream stream = await MyQuery(parameters)) using (StreamReader reader = new StreamReader(stream)) { string conten

我正在查询远程服务器,有时会得到一个
aggregateeexception
。这是相当罕见的,我知道发生这种情况时如何处理,但出于某种原因,每当抛出异常时,它不会进入
catch

这是catch块的代码部分:

try
{
    using (Stream stream = await MyQuery(parameters))
    using (StreamReader reader = new StreamReader(stream))
    {
        string content = reader.ReadToEnd();
        return content;
    }
}
catch (AggregateException exception)
{
    exception.Handle((innerException) =>
    {
        if (innerException is IOException && innerException.InnerException is SocketException)
        {
            DoSomething();
            return true;
        }
        return false;
    });
}
这是我得到的例外信息:

System.AggregateException: One or more errors occurred. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
我假设-->箭头表示这是一个内部异常,对吗?
因此,如果它是IOException->SocketException,为什么从未调用过
DoSomething()

我怀疑您此时实际上没有看到
AggregateException
。在你的代码中没有任何东西可以做并行操作

如果这是正确的,您应该能够执行以下操作:

try
{
  using (Stream stream = await MyQuery(parameters))
  using (StreamReader reader = new StreamReader(stream))
  {
    string content = reader.ReadToEnd();
    return content;
  }
}
catch (IOException exception)
{
  if (exception.InnerException is SocketException)
    DoSomething();
  else
    throw;
}

你在调试器中看过这个吗?没有。。正如我所说,这种情况并不经常发生。那么,我至少要添加一些日志记录……我认为有人比我更了解我,但
async/wait
模式有一个底层状态机,它为您“打开”了
aggregateeexception
,并抛出原始类型的异常。因此,您希望只捕获
IOException
,而不是
aggregateeexception