Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 62d5964

Browse files
svickPaulo Janotti
authored andcommitted
XmlReader.Read shouldn't throw AggregateException (#25681)
* XmlReader.Read shouldn't throw AggregateException * Remove unnecessary uses of Task.Result * Address feedback * Test is skipped on .Net Framework
1 parent a5cc306 commit 62d5964

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Globalization;
1010
using System.Collections.Generic;
1111
using System.Runtime.CompilerServices;
12+
using System.Threading.Tasks;
1213

1314
namespace System.Xml
1415
{
@@ -624,9 +625,8 @@ private void FinishInitUriString()
624625
{
625626
//this will be hit when user create a XmlReader by setting Async, but the first call is Read() instead of ReadAsync(),
626627
//then we still should create an async stream here. And wait for the method finish.
627-
System.Threading.Tasks.Task<object> t = _laterInitParam.inputUriResolver.GetEntityAsync(_laterInitParam.inputbaseUri, string.Empty, typeof(Stream));
628-
t.Wait();
629-
stream = (Stream)t.Result;
628+
Task<object> t = _laterInitParam.inputUriResolver.GetEntityAsync(_laterInitParam.inputbaseUri, string.Empty, typeof(Stream));
629+
stream = (Stream)t.GetAwaiter().GetResult();
630630
}
631631
else
632632
{

src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ public override async Task SkipAsync()
367367

368368
private async Task<int> ReadContentAsBase64_AsyncHelper(Task<bool> task, byte[] buffer, int index, int count)
369369
{
370-
await task.ConfigureAwait(false);
371-
if (!task.Result)
370+
bool result = await task.ConfigureAwait(false);
371+
if (!result)
372372
{
373373
return 0;
374374
}
@@ -514,8 +514,8 @@ public override async Task<int> ReadContentAsBinHexAsync(byte[] buffer, int inde
514514

515515
private async Task<int> ReadElementContentAsBase64Async_Helper(Task<bool> task, byte[] buffer, int index, int count)
516516
{
517-
await task.ConfigureAwait(false);
518-
if (!task.Result)
517+
bool result = await task.ConfigureAwait(false);
518+
if (!result)
519519
{
520520
return 0;
521521
}

src/System.Private.Xml/tests/XmlReader/Tests/AsyncReaderLateInitTests.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public static void ReadAsyncAfterInitializationWithStreamDoesNotThrow()
3939
}
4040
}
4141

42-
[Fact]
43-
public static void ReadAfterInitializationWithStreamOnAsyncReaderDoesNotThrow()
42+
[Theory, InlineData(true), InlineData(false)]
43+
public static void ReadAfterInitializationWithStreamOnAsyncReaderDoesNotThrow(bool async)
4444
{
45-
using (XmlReader reader = XmlReader.Create(GetDummyXmlStream(), new XmlReaderSettings() { Async = true }))
45+
using (XmlReader reader = XmlReader.Create(GetDummyXmlStream(), new XmlReaderSettings() { Async = async }))
4646
{
4747
reader.Read();
4848
}
@@ -57,10 +57,10 @@ public static void ReadAsyncAfterInitializationWithTextReaderDoesNotThrow()
5757
}
5858
}
5959

60-
[Fact]
61-
public static void ReadAfterInitializationWithTextReaderOnAsyncReaderDoesNotThrow()
60+
[Theory, InlineData(true), InlineData(false)]
61+
public static void ReadAfterInitializationWithTextReaderOnAsyncReaderDoesNotThrow(bool async)
6262
{
63-
using (XmlReader reader = XmlReader.Create(GetDummyXmlTextReader(), new XmlReaderSettings() { Async = true }))
63+
using (XmlReader reader = XmlReader.Create(GetDummyXmlTextReader(), new XmlReaderSettings() { Async = async }))
6464
{
6565
reader.Read();
6666
}
@@ -75,14 +75,19 @@ public static void ReadAsyncAfterInitializationWithUriThrows()
7575
}
7676
}
7777

78-
[Fact]
78+
[Fact, SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".Net Framework throws AggregateException")]
7979
public static void ReadAfterInitializationWithUriOnAsyncReaderTrows()
8080
{
8181
using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true }))
8282
{
83-
AggregateException ae = Assert.Throws<System.AggregateException>(() => reader.Read());
84-
Assert.Equal(typeof(System.Net.WebException), ae.InnerException.GetType());
83+
Assert.Throws<System.Net.WebException>(() => reader.Read());
8584
}
8685
}
86+
87+
[Fact]
88+
public static void InitializationWithUriOnNonAsyncReaderTrows()
89+
{
90+
Assert.Throws<System.Net.WebException>(() => XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = false }));
91+
}
8792
}
8893
}

0 commit comments

Comments
 (0)