I have wrote a simple service to serialize and deserialize classes with the default XmlSerializer. I wanted to do this async but when the XMlSerializer throws an exception inside the Task, it is not caught by the try/catch
public async Task<T> DeserializeAsync<T>(TextReader reader)
{
try
{
return await Task.Run(() =>
{
var serializer = new XmlSerializer(typeof(T));
var result = serializer.Deserialize(reader);
return (T) result;
});
}
catch (Exception e)
{
//Do something with exception
}
}
I have one solution but it can't possibly be the solution to this problem:
public async Task<T> DeserializeAsync<T>(TextReader reader)
{
Exception exception = null;
var result = await Task.Run(
() =>
{
try
{
return (T) new XmlSerializer(typeof(T)).Deserialize(reader);
}
catch (Exception e)
{
exception = e;
return default(T);
}
});
if (exception != null)
{
// handle exception
}
return result;
}
UPDATE: Code that reproduces the error:
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
public class Program
{
private static Serializer _serializer;
static void Main(string[] args)
{
_serializer = new Serializer();
Task.Factory.StartNew(ReadUsers);
Console.ReadKey();
}
private static async Task ReadUsers()
{
var stream = new MemoryStream(Encoding.ASCII.GetBytes(""));
try
{
var user = await _serializer.DeserializeAsync<User>(new StreamReader(stream));
Console.WriteLine(user.Name);
}
catch (Exception e)
{
Console.WriteLine($"Caught exception: {e.Message}");
}
}
}
public class Serializer
{
public async Task<T> DeserializeAsync<T>(TextReader reader)
=> await Task.Run(() => (T) new XmlSerializer(typeof(T)).Deserialize(reader));
}
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}