ExecuteXmlReaderAsync is not working as expected

I am trying to implement reading XML from MSSQL using new async methods in .NET 4.5.

I have the following code

var xmlReader = await sqlCommand.ExecuteXmlReaderAsync();

while (await xmlReader.ReadAsync())
{
    var doc = (MyDocument)xmlSerializer.Deserialize(xmlReader);
    await Process(doc);
}

      

and fail Set XmlReaderSettings.Async to true if you want to use Async methods. ...

I have checked the decompiled sources of ExecuteXmlReaderAsync and I can see that it uses one of these parameters:

private static readonly XmlReaderSettings DefaultXmlReaderSettings = new XmlReaderSettings()
{
  ConformanceLevel = ConformanceLevel.Fragment
};
private static readonly XmlReaderSettings DefaultXmlReaderSettingsCloseInput = new       XmlReaderSettings()
{
  ConformanceLevel = ConformanceLevel.Fragment,
  CloseInput = true
};

      

it looks like ExecuteXmlReaderAsync is not well implemented.

Please consult if you have experience with this method.

UPD: xmlReader.Settings.Async is readonly, so it must be set when the object is created

+3


source to share


1 answer


I think you know exactly the source of this problem. If I use reflection to change the settings on the private field you mentioned, it works.

var settingsField = typeof(SqlXml)
    .GetField("DefaultXmlReaderSettingsCloseInput",
              BindingFlags.Static | BindingFlags.NonPublic);
var settings = (XmlReaderSettings)settingsField.GetValue(null);
settings.Async = true;

      



Please note that this is a dirty hack that can stop working at any time. And I really don't know what I am doing, so it might not work as expected, or it might have some unintended consequences. Because of this, I would be very careful when using this.

Also, I think you should report this as a bug to Microsoft .

+2


source







All Articles