I have an exception class that extends Exception that is used to deserialize an exception response from an external API.
//Also tried without constructor call, same result
//[Serializable]
[Serializable()]
public sealed class ServiceCoreException : Exception, ISerializable
//Also tried without implementing ISerializable (doesn't work)
{
public ServiceCoreException () : base(){}
public ServiceCoreException (string message) : base(message) { }
public ServiceCoreException (string message, Exception innerException) : base(message, innerException) { }
public ServiceCoreException (string errorCode, string message, string traceId, string exceptionType) : base(message)
{
ErrorCode = errorCode;
TraceId = traceId;
ExceptionType = exceptionType;
}
public ServiceCoreException (string errorCode, string message, string traceId, string exceptionType, Exception innerException) : base(message, innerException)
{
ErrorCode = errorCode;
TraceId = traceId;
ExceptionType = exceptionType;
}
protected ServiceCoreException (SerializationInfo info, StreamingContext context) : base(info, context)
{
TraceId = info.GetString("TraceId");
ErrorCode = info.GetString("ErrorCode");
ExceptionType = info.GetString("ExceptionType");
//Test attempt, throws an "Cannot add the same member twice to a SerializationInfo object" exception
//base.GetObjectData(info, context);
}
//Here for testing purpose, doesn't actually run...
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
public string TraceId { get; set; }
public string ErrorCode { get; set; }
public string ExceptionType { get; set; }
}
The above implementation is based on numerous answers found on here (for example this) and following the example on MSDN here attempting to fix getting the following exception:
ISerializable type 'Hidden.Namespace.ServiceCoreException' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.
but results in getting the exception "System.Runtime.Serialization.SerializationException: Member 'ClassName' was not found".
The incoming raw data is (cleaned up to hide certain info):
{
"ErrorCode": "DUPLICATION_ERROR",
"Message": "Duplicate Entries Found",
"TraceId": "00000000-0000-0000-0000-000000000000",
"ExceptionType": "CoreServiceException"
}
Answers i've found so far are unhelpful to say the least along the lines of "your code is wrong, do this" which also doesn't work.
Help solving this would be appreciated as i feel i've tried everything at this point.