Why do I need to set XmlWriterSettings.Async to true to use async methods?

1.2k views Asked by At

It's been a while since I last used XmlWriter but since today has been a lucky day to write some XML, so I started to use all the ...Async methods by default but straight away I get the following exception:

System.InvalidOperationException : Set XmlWriterSettings.Async to true if you want to use Async Methods.

It seems a bit weird to me that I have to set a flag on a settings object that I pass into the constructor of XmlWriter so that I can open up the use of methods on a class.

For example:

using (var xmlWriter = XmlWriter.Create(stringBuilder))
{
    await xmlWriter.WriteStartElementAsync("", "root", "")
        .ConfigureAwait(false);
}

Will throw an exception, however:

using (var xmlWriter = XmlWriter.Create(stringBuilder, new XmlWriterSettings(){Async = true}))
{
    await xmlWriter.WriteStartElementAsync("", "root", "")
        .ConfigureAwait(false);
}

Will work fine.

Does anyone know the reasoning behind this?

0

There are 0 answers