I have a custom exception:
[Serializable]
public class InternalException : Exception, ISerializable
{
public InternalException()
: base()
{ }
public InternalException(string message)
: base(message)
{ }
public InternalException(string message, Exception innerException)
: base(message, innerException)
{ }
protected InternalException(SerializationInfo info, StreamingContext context)
{ }
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
}
I am doing this so that I can wrap 3rd party exceptions, and I add instances in a memory cache server-side. The client then gets the exception by doing:
var resp = await client.GetAsync("url");
If I do the following:
throw await resp.Content.ReadAsAsync<InternalException>(ct);
I get an exception The member ClassName could not be found
. So I tried:
var str = await resp.Content.ReadAsStringAsync();
var ex = Convert.DeserializeObject<InternalException>(str);
throw ex;
str
is the correctly deserialized exception. However, when I do the above, ex
is a completely new exception and its message is An exception of type "InternalException" was thrown
.
Why is a new exception being created and how do I actually deserialize the original exception and throw it successfully?
Note: This is an internal api and client and I do need to return exceptions to the client.