Skip to content

Commit 8601b68

Browse files
authored
[5.1.1] Fix | NullReferenceException in GetBytesAsync (#1964)
1 parent 41af66f commit 8601b68

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5285,7 +5285,7 @@ protected override void DisposeCore()
52855285
{
52865286
TDisposable copy = _disposable;
52875287
_disposable = default;
5288-
copy.Dispose();
5288+
copy?.Dispose();
52895289
}
52905290
}
52915291

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderStreamsTest.cs

+32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text;
1010
using System.Threading.Tasks;
1111
using System.Xml;
12+
using System.Xml.Linq;
1213
using Xunit;
1314

1415
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
@@ -471,6 +472,37 @@ public static void InvalidCastExceptionStream(CommandBehavior behavior, Accessor
471472
}
472473
}
473474

475+
#if NETCOREAPP
476+
[ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
477+
public static async void ReadAsyncContentsCompletes()
478+
{
479+
string expectedXml = "<test>This is a test string</test>";
480+
string query = $"SELECT CAST('{expectedXml}' AS NVARCHAR(MAX))";
481+
482+
string returnedXml = null;
483+
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
484+
using (SqlCommand command = new SqlCommand(query, connection))
485+
{
486+
connection.Open();
487+
488+
await using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess).ConfigureAwait(false))
489+
{
490+
while (await reader.ReadAsync().ConfigureAwait(false))
491+
{
492+
using (TextReader textReader = reader.GetTextReader(0))
493+
using (XmlReader xmlReader = XmlReader.Create(textReader, new XmlReaderSettings() { Async = true }))
494+
{
495+
XDocument xdoc = await XDocument.LoadAsync(xmlReader, LoadOptions.None, default).ConfigureAwait(false);
496+
returnedXml = xdoc.ToString();
497+
}
498+
}
499+
}
500+
}
501+
502+
Assert.Equal(expectedXml, returnedXml, StringComparer.Ordinal);
503+
}
504+
#endif
505+
474506
private static async Task<SqlDataReader> ExecuteReader(SqlCommand command, CommandBehavior behavior, bool isExecuteAsync)
475507
=> isExecuteAsync ? await command.ExecuteReaderAsync(behavior) : command.ExecuteReader(behavior);
476508

0 commit comments

Comments
 (0)