diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs index 1b9001c8ee..e19ee3eba0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -1779,7 +1779,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult) try { success = true; - return CompleteXmlReader(InternalEndExecuteReader(asyncResult, false, nameof(EndExecuteXmlReader))); + return CompleteXmlReader(InternalEndExecuteReader(asyncResult, false, nameof(EndExecuteXmlReader)), true); } catch (Exception e) { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs index 35c260c05b..d19c2b16bd 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -2265,7 +2265,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult) int? sqlExceptionNumber = null; try { - XmlReader result = CompleteXmlReader(InternalEndExecuteReader(asyncResult, ADP.EndExecuteXmlReader, isInternal: false)); + XmlReader result = CompleteXmlReader(InternalEndExecuteReader(asyncResult, ADP.EndExecuteXmlReader, isInternal: false), true); success = true; return result; } @@ -2299,7 +2299,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult) } } - private XmlReader CompleteXmlReader(SqlDataReader ds) + private XmlReader CompleteXmlReader(SqlDataReader ds, bool async = false) { XmlReader xr = null; @@ -2313,7 +2313,7 @@ private XmlReader CompleteXmlReader(SqlDataReader ds) try { SqlStream sqlBuf = new SqlStream(ds, true /*addByteOrderMark*/, (md[0].SqlDbType == SqlDbType.Xml) ? false : true /*process all rows*/); - xr = sqlBuf.ToXmlReader(); + xr = sqlBuf.ToXmlReader(async); } catch (Exception e) { diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs index 52a5dde8f3..9d425b1b75 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs @@ -4,13 +4,14 @@ using System; using System.Xml; +using System.Xml.Linq; using Xunit; namespace Microsoft.Data.SqlClient.ManualTesting.Tests { public static class XmlReaderAsyncTest { - private static string commandText = + private const string CommandText = "SELECT * from dbo.Customers FOR XML AUTO, XMLDATA;"; // Synapse: Parse error at line: 1, column: 29: Incorrect syntax near 'FOR'. @@ -18,8 +19,8 @@ public static class XmlReaderAsyncTest public static void ExecuteTest() { using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString)) + using (SqlCommand command = new SqlCommand(CommandText, connection)) { - SqlCommand command = new SqlCommand(commandText, connection); connection.Open(); IAsyncResult result = command.BeginExecuteXmlReader(); @@ -28,10 +29,13 @@ public static void ExecuteTest() System.Threading.Thread.Sleep(100); } - XmlReader reader = command.EndExecuteXmlReader(result); - - reader.ReadToDescendant("dbo.Customers"); - Assert.Equal("ALFKI", reader["CustomerID"]); + using (XmlReader xmlReader = command.EndExecuteXmlReader(result)) + { + // Issue #781: Test failed here as xmlReader.Settings.Async was set to false + Assert.True(xmlReader.Settings.Async); + xmlReader.ReadToDescendant("dbo.Customers"); + Assert.Equal("ALFKI", xmlReader["CustomerID"]); + } } } @@ -40,8 +44,8 @@ public static void ExecuteTest() public static void ExceptionTest() { using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString)) + using (SqlCommand command = new SqlCommand(CommandText, connection)) { - SqlCommand command = new SqlCommand(commandText, connection); connection.Open(); //Try to execute a synchronous query on same command @@ -55,10 +59,39 @@ public static void ExceptionTest() System.Threading.Thread.Sleep(100); } - XmlReader reader = command.EndExecuteXmlReader(result); + using (XmlReader xmlReader = command.EndExecuteXmlReader(result)) + { + // Issue #781: Test failed here as xmlReader.Settings.Async was set to false + Assert.True(xmlReader.Settings.Async); + xmlReader.ReadToDescendant("dbo.Customers"); + Assert.Equal("ALFKI", xmlReader["CustomerID"]); + } + } + } + + // Synapse: Parse error at line: 1, column: 29: Incorrect syntax near 'FOR'. + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))] + public static async void MoveToContentAsyncTest() + { + using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString)) + using (SqlCommand command = new SqlCommand(CommandText, connection)) + { + connection.Open(); - reader.ReadToDescendant("dbo.Customers"); - Assert.Equal("ALFKI", reader["CustomerID"]); + using (XmlReader xmlReader = await command.ExecuteXmlReaderAsync().ConfigureAwait(false)) + { + try + { + // Issue #781: Test failed here as xmlReader.Settings.Async was set to false + Assert.True(xmlReader.Settings.Async); + xmlReader.ReadToDescendant("dbo.Customers"); + Assert.Equal("ALFKI", xmlReader["CustomerID"]); + } + catch (Exception ex) + { + Assert.False(true, "Exception occurred: " + ex.Message); + } + } } } }