Skip to content

Commit

Permalink
[5.1.1] Fix | NullReferenceException in GetBytesAsync (#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcheunglci authored Mar 27, 2023
1 parent 41af66f commit 8601b68
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5285,7 +5285,7 @@ protected override void DisposeCore()
{
TDisposable copy = _disposable;
_disposable = default;
copy.Dispose();
copy?.Dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Xunit;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
Expand Down Expand Up @@ -471,6 +472,37 @@ public static void InvalidCastExceptionStream(CommandBehavior behavior, Accessor
}
}

#if NETCOREAPP
[ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static async void ReadAsyncContentsCompletes()
{
string expectedXml = "<test>This is a test string</test>";
string query = $"SELECT CAST('{expectedXml}' AS NVARCHAR(MAX))";

string returnedXml = null;
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();

await using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess).ConfigureAwait(false))
{
while (await reader.ReadAsync().ConfigureAwait(false))
{
using (TextReader textReader = reader.GetTextReader(0))
using (XmlReader xmlReader = XmlReader.Create(textReader, new XmlReaderSettings() { Async = true }))
{
XDocument xdoc = await XDocument.LoadAsync(xmlReader, LoadOptions.None, default).ConfigureAwait(false);
returnedXml = xdoc.ToString();
}
}
}
}

Assert.Equal(expectedXml, returnedXml, StringComparer.Ordinal);
}
#endif

private static async Task<SqlDataReader> ExecuteReader(SqlCommand command, CommandBehavior behavior, bool isExecuteAsync)
=> isExecuteAsync ? await command.ExecuteReaderAsync(behavior) : command.ExecuteReader(behavior);

Expand Down

0 comments on commit 8601b68

Please sign in to comment.