0

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.

  • Not sure if this could be of help, did you try what's mentioned [here](https://github.com/JamesNK/Newtonsoft.Json/issues/1622)? – Andrew Jan 10 '22 at 17:02
  • 1
    You nowhere state what kind of API that is and how the text resembling an Exception gets actually deserialized. That might be an internal part of the used framework for that API type (REST Api with Asp.Net core for example). But without really knowing we don't know. – Ralf Jan 10 '22 at 17:03
  • Can you post the exception, any inner exceptions, and the stack trace as text? It seems suspicious that the exception refers to "ClassName" even though it's nowhere in the code. I don't know if that's because something is missing or if the framework code normally looks for something called "ClassName." – Scott Hannen Jan 10 '22 at 17:06

0 Answers0