Closed amartini-n closed 4 years ago
The issue has been earlier posted on Stack Overflow: https://stackoverflow.com/questions/60726123/invalidcastexception-deserializing-a-sqlexception-with-newtonsoft-json.
SqlException
comes with ClientConnectionId
property of type Guid
. The error is raised in the constructor of the class with the (SerializationInfo si, StreamingContext sc)
signature (see the implementation here below:
private SqlException(SerializationInfo si, StreamingContext sc) : base(si, sc)
{
HResult = SqlExceptionHResult;
foreach (SerializationEntry siEntry in si)
{
if ("ClientConnectionId" == siEntry.Name)
{
_clientConnectionId = (Guid)siEntry.Value;
break;
}
}
}
I guess the issue is not in this constructor but in the reason why siEntry.Value
is set to an instance of a type that could not be casted to a Guid
(e.g. string
).
Sorry, this issue is nonsense. There's no way for Newtonsoft JSON library to provide something different from JValue
instances as values of SerializationEntry
items (of SerializationInfo
instance in ISerializable
constructor). It is the constructor of target class implementing ISerializable
that has the knowledge of itself and that should be able to handle SerializationEntry
items differently e.g. by first of all testing if they are instances of the target type or they implementIConvertible
.
For the reason, I'm closing the issue.
Environment information
.NET Core: version 2.2 Newtonsoft.Json: version 11.0.1
Destination types
Source JSON
Expected behavior
I should be able to deserialize previous JSON string to an instance of SqlException by calling JsonConvert.DeserializeObject() and
TypeNameHandling
setting set toAll
.Actual behavior
I got following exception:
Steps to reproduce