Moo-Juice
Moo-Juice

Reputation: 38825

Trouble Serializing an Exception with ServiceStack

When an exception is thrown in my framework and a log entry is generated, I want to serialize the exception and throw it in to the database. ServiceStack.Text.ToJson<> works on all my POCOs, but in the following code:

new Exception("Hello").ToJson()

Results in:

"\"Hello\""

...rather than the full exception. Is there anything obvious I am missing?

Edit

Just to clarify, the above is just the minimal code needed to reproduce the behaviour. The actual code is called when an exception is caught, and the same thing happens.

Upvotes: 1

Views: 234

Answers (1)

Moo-Juice
Moo-Juice

Reputation: 38825

There may be a very good reason why ServiceStack does that for exceptions. I wrote a quick serializer that iterated through public, readable, properties of a object, in a deep sense - and I believe I got a stack overflow on Exception thanks to all the Assembly, declaring type, method-type stuff. I think it was an overflow of some sort because the test program blew a gasket.

Whether or not this observation is correct - it quickly became apparent that one would not want to store an complete serialized Exception in the database. The useful information (exception name, inner exception, stack trace, etc) is indeed useful - however there is so much extraneous stuff you simply wouldn't need nor is desirable if you tried to serialize the exception as a whole.

I suspect this is why ServiceStack employs that ResponseStatus object which keeps a few pertinent details. I have elected to do something similar, which keeps the stack trace and the inner exception - but pulls it in to a much simpler POCO without everything else.

Upvotes: 2

Related Questions