In regard to the XmlSerializer I wonder if for a WebAPI it matters if the operation in this case is sync or async. The point is I haven't really saw anyone using ReadToEndAsync, so far what I have saw is everyone uses the sync version. There for my question which one am I supposed to use. Maybe this question depends on the context if we are looking the hole web API or maybe if we are looking one specific request and inside that request we have multiple parts that we want to deserialise.
Maybe since my scenario takes multiple strings and tries them to parse into xml I should use ReadToEndAsync + WhenAll to get better performance?
Please if possible give me some clarification on the pros and cons.
public static async Task<string> SerializeToXmlDefaultAsync(T value, Type type = null)
{
var settings = new XmlWriterSettings
{
Indent = true,
Async = true
};
var ms = new MemoryStream();
using var writer = XmlWriter.Create(ms, settings);
XmlSerializer cs;
if (type == null)
cs = new XmlSerializer(typeof(T));
else cs = new XmlSerializer(type);
cs.Serialize(writer, value);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
var sr = new StreamReader(ms);
return await sr.ReadToEndAsync(); // Should this be async and why?
}