Bug #93825 MySqlException is marked [Serializable] but doesn't persist data
Submitted: 7 Jan 2019 0:38 Modified: 7 Jan 2019 5:20
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.13 OS:Windows (10)
Assigned to: CPU Architecture:Other (x64)

[7 Jan 2019 0:38] Bradley Grainger
Description:
The MySqlException class is marked [Serializable], but doesn't serialize/deserialize the custom properties it adds, i.e., Number and SqlState.

All class data should be transferred across a serialization boundary.

How to repeat:
Compile and run the following C# code. It prints:
1042
0

The expected output is:
1042
1042

// generate an exception with MySqlException.Number set
MySqlException exception = null;
using (var connection = new MySqlConnection("server=localhost;port=1"))
{
	try
	{
		connection.Open();
	}
	catch (MySqlException e)
	{
		exception = e;
	}
}

// serialize and deserialize the exception
MySqlException copy = null;
using (var stream = new MemoryStream())
{
	var formatter = new BinaryFormatter();
	formatter.Serialize(stream, exception);
	stream.Position = 0;
	copy = (MySqlException) formatter.Deserialize(stream);
}

// the properties aren't preserved across serialization
Console.WriteLine(exception.Number); // 1042
Console.WriteLine(copy.Number); // 0

Suggested fix:
Initialize property values from the SerializationInfo in the constructor, then implement GetObjectData and save the values of the properties to the provided SerializationInfo.
[7 Jan 2019 5:20] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.
Observed with VS 2017 (C#.Net) and Connector/NET 8.0.13 version.

regards,
Umesh