2

My WebApi throws an internal server exception, and in the client I'm trying to deserialize back the result in an exception.

The controller's action does:

try
{
    //Do something...
}
catch (Exception ex)
{
    return InternalServerError(ex);
}

And in the client I'm doing:

var content = response.Content.ReadAsStringAsync().Result;
var exception = JsonConvert.DeserializeObject<Exception>(content);

But an exception is thrown:

Member 'ClassName' was not found.

Checking the context with Json2csharp, I get the following objects:

public class InnerException
{
    public string Message { get; set; }
    public string ExceptionMessage { get; set; }
    public string ExceptionType { get; set; }
    public string StackTrace { get; set; }
}

public class RootObject
{
    public string Message { get; set; }
    public string ExceptionMessage { get; set; }
    public string ExceptionType { get; set; }
    public string StackTrace { get; set; }
    public InnerException InnerException { get; set; }
}

So where is the ClassName coming from, and what is the best way to deserialize an exception?

EDIT

To serialize the exception I created a new class derived from Exception, and added the following code:

    protected RequestException(SerializationInfo info, StreamingContext context)
    {
    }

    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
    }

However, the original exception details, especially the inner exception, are lost.

5
  • looks that the serialized exception is of derived type, so it has more properties than the type Exception, therefor it cannot be deserialized this way - you must provide exact type of the deserialized exception to the deserializer Commented Apr 22, 2016 at 11:13
  • 1
    I don't know C# but typically you wouldn't return a raw server-side exception to the front end - since you are catching it, why not return something more user friendly that doesn't expose your class names and server side stack trace? Commented Apr 22, 2016 at 11:14
  • can you post your JSON string ? Commented Apr 22, 2016 at 11:19
  • what happens when you add var exception = JsonConvert.DeserializeObject<YourDerivedType>(content); Commented Apr 22, 2016 at 11:25
  • oh, the inner exception can be a problem, if the inner exception is present, deserializer cannot know what type to deserialize it to Commented Apr 22, 2016 at 11:30

1 Answer 1

2

do not expose exception itself, create your own response object with inner properties, in case of exception fill your own object and pass it to response. on client side use

var content = response.Content.ReadAsStringAsync().Result;
var response = JsonConvert.DeserializeObject<MyResponseObject>(content);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.