Skip to content

Commit 34d6810

Browse files
Fix bug in Async ExecuteXmlReader implementation
Fixes #781
1 parent ab9fbb7 commit 34d6810

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult)
17791779
try
17801780
{
17811781
success = true;
1782-
return CompleteXmlReader(InternalEndExecuteReader(asyncResult, false, nameof(EndExecuteXmlReader)));
1782+
return CompleteXmlReader(InternalEndExecuteReader(asyncResult, false, nameof(EndExecuteXmlReader)), true);
17831783
}
17841784
catch (Exception e)
17851785
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult)
22652265
int? sqlExceptionNumber = null;
22662266
try
22672267
{
2268-
XmlReader result = CompleteXmlReader(InternalEndExecuteReader(asyncResult, ADP.EndExecuteXmlReader, isInternal: false));
2268+
XmlReader result = CompleteXmlReader(InternalEndExecuteReader(asyncResult, ADP.EndExecuteXmlReader, isInternal: false), true);
22692269
success = true;
22702270
return result;
22712271
}
@@ -2299,7 +2299,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult)
22992299
}
23002300
}
23012301

2302-
private XmlReader CompleteXmlReader(SqlDataReader ds)
2302+
private XmlReader CompleteXmlReader(SqlDataReader ds, bool async = false)
23032303
{
23042304
XmlReader xr = null;
23052305

@@ -2313,7 +2313,7 @@ private XmlReader CompleteXmlReader(SqlDataReader ds)
23132313
try
23142314
{
23152315
SqlStream sqlBuf = new SqlStream(ds, true /*addByteOrderMark*/, (md[0].SqlDbType == SqlDbType.Xml) ? false : true /*process all rows*/);
2316-
xr = sqlBuf.ToXmlReader();
2316+
xr = sqlBuf.ToXmlReader(async);
23172317
}
23182318
catch (Exception e)
23192319
{

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44

55
using System;
66
using System.Xml;
7+
using System.Xml.Linq;
78
using Xunit;
89

910
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
1011
{
1112
public static class XmlReaderAsyncTest
1213
{
13-
private static string commandText =
14+
private const string CommandText =
1415
"SELECT * from dbo.Customers FOR XML AUTO, XMLDATA;";
1516

1617
// Synapse: Parse error at line: 1, column: 29: Incorrect syntax near 'FOR'.
1718
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
1819
public static void ExecuteTest()
1920
{
2021
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
22+
using (SqlCommand command = new SqlCommand(CommandText, connection))
2123
{
22-
SqlCommand command = new SqlCommand(commandText, connection);
2324
connection.Open();
2425

2526
IAsyncResult result = command.BeginExecuteXmlReader();
@@ -28,10 +29,13 @@ public static void ExecuteTest()
2829
System.Threading.Thread.Sleep(100);
2930
}
3031

31-
XmlReader reader = command.EndExecuteXmlReader(result);
32-
33-
reader.ReadToDescendant("dbo.Customers");
34-
Assert.Equal("ALFKI", reader["CustomerID"]);
32+
using (XmlReader xmlReader = command.EndExecuteXmlReader(result))
33+
{
34+
// Issue #781: Test failed here as xmlReader.Settings.Async was set to false
35+
Assert.True(xmlReader.Settings.Async);
36+
xmlReader.ReadToDescendant("dbo.Customers");
37+
Assert.Equal("ALFKI", xmlReader["CustomerID"]);
38+
}
3539
}
3640
}
3741

@@ -40,8 +44,8 @@ public static void ExecuteTest()
4044
public static void ExceptionTest()
4145
{
4246
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
47+
using (SqlCommand command = new SqlCommand(CommandText, connection))
4348
{
44-
SqlCommand command = new SqlCommand(commandText, connection);
4549
connection.Open();
4650

4751
//Try to execute a synchronous query on same command
@@ -55,10 +59,39 @@ public static void ExceptionTest()
5559
System.Threading.Thread.Sleep(100);
5660
}
5761

58-
XmlReader reader = command.EndExecuteXmlReader(result);
62+
using (XmlReader xmlReader = command.EndExecuteXmlReader(result))
63+
{
64+
// Issue #781: Test failed here as xmlReader.Settings.Async was set to false
65+
Assert.True(xmlReader.Settings.Async);
66+
xmlReader.ReadToDescendant("dbo.Customers");
67+
Assert.Equal("ALFKI", xmlReader["CustomerID"]);
68+
}
69+
}
70+
}
71+
72+
// Synapse: Parse error at line: 1, column: 29: Incorrect syntax near 'FOR'.
73+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
74+
public static async void MoveToContentAsyncTest()
75+
{
76+
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
77+
using (SqlCommand command = new SqlCommand(CommandText, connection))
78+
{
79+
connection.Open();
5980

60-
reader.ReadToDescendant("dbo.Customers");
61-
Assert.Equal("ALFKI", reader["CustomerID"]);
81+
using (XmlReader xmlReader = await command.ExecuteXmlReaderAsync().ConfigureAwait(false))
82+
{
83+
try
84+
{
85+
// Issue #781: Test failed here as xmlReader.Settings.Async was set to false
86+
Assert.True(xmlReader.Settings.Async);
87+
xmlReader.ReadToDescendant("dbo.Customers");
88+
Assert.Equal("ALFKI", xmlReader["CustomerID"]);
89+
}
90+
catch (Exception ex)
91+
{
92+
Assert.False(true, "Exception occurred: " + ex.Message);
93+
}
94+
}
6295
}
6396
}
6497
}

0 commit comments

Comments
 (0)