Skip to content

Commit 2e9fff8

Browse files
committed
Make MySqlException serializable. Fixes #601
1 parent 17fb5fe commit 2e9fff8

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

docs/content/tutorials/migrating-from-connector-net.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,4 @@ The following bugs in Connector/NET are fixed by switching to MySqlConnector. (~
146146
* [#93220](https://bugs.mysql.com/bug.php?id=93220): Can’t call FUNCTION when parameter name contains parentheses
147147
* [#93370](https://bugs.mysql.com/bug.php?id=93370): `MySqlParameterCollection.Add` precondition check isn't consistent
148148
* [#93374](https://bugs.mysql.com/bug.php?id=93374): `MySqlDataReader.GetStream` throws `IndexOutOfRangeException`
149+
* [#93825](https://bugs.mysql.com/bug.php?id=93825): `MySqlException` loses data when serialized

src/MySqlConnector/MySql.Data.MySqlClient/MySqlException.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
using System;
22
using System.Data.Common;
3+
#if !NETSTANDARD1_3
4+
using System.Runtime.Serialization;
5+
#endif
36

47
namespace MySql.Data.MySqlClient
58
{
9+
#if !NETSTANDARD1_3
10+
[Serializable]
11+
#endif
612
public sealed class MySqlException : DbException
713
{
814
public int Number { get; }
915
public string SqlState { get; }
1016

17+
#if !NETSTANDARD1_3
18+
private MySqlException(SerializationInfo info, StreamingContext context)
19+
: base(info, context)
20+
{
21+
Number = info.GetInt32("Number");
22+
SqlState = info.GetString("SqlState");
23+
}
24+
25+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
26+
{
27+
base.GetObjectData(info, context);
28+
info.AddValue("Number", Number);
29+
info.AddValue("SqlState", SqlState);
30+
}
31+
#endif
32+
1133
internal MySqlException(string message)
1234
: this(message, null)
1335
{

tests/MySqlConnector.Tests/MySqlConnector.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<ItemGroup Condition=" '$(Configuration)' == 'Baseline' ">
3434
<PackageReference Include="MySql.Data" Version="8.0.13" />
35-
<Compile Remove="ByteBufferWriterTests.cs;CachedProcedureTests.cs;ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;NormalizeTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;UtilityTests.cs" />
35+
<Compile Remove="ByteBufferWriterTests.cs;CachedProcedureTests.cs;ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;MySqlExceptionTests.cs;NormalizeTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;UtilityTests.cs" />
3636
</ItemGroup>
3737

3838
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.IO;
2+
using System.Runtime.Serialization.Formatters.Binary;
3+
using MySql.Data.MySqlClient;
4+
using Xunit;
5+
6+
namespace MySqlConnector.Tests
7+
{
8+
public class MySqlExceptionTests
9+
{
10+
[Fact]
11+
public void IsSerializable()
12+
{
13+
var exception = new MySqlException(1, "two", "three");
14+
MySqlException copy;
15+
16+
using (var stream = new MemoryStream())
17+
{
18+
var formatter = new BinaryFormatter();
19+
formatter.Serialize(stream, exception);
20+
stream.Position = 0;
21+
copy = (MySqlException) formatter.Deserialize(stream);
22+
}
23+
24+
Assert.Equal(exception.Number, copy.Number);
25+
Assert.Equal(exception.SqlState, copy.SqlState);
26+
Assert.Equal(exception.Message, copy.Message);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)