0

I have a custom web service that when an exception happens I serialize up the exception chain and send it to the client, now that works fine. However what I'd like to do on the client side is recreate the exception chain somehow and throw it, so that the user can walk it like they could on the server side.

I'm not expecting to be able to throw exceptions I don't know about, I was thinking about throwing a chain of WrappedException or some such with stack, message etc I have set.

Does anyone know any way to do this?

To avoid any confusion this is completely custom, I make requests in xml to a web server and get xml back, if an exception is thrown I serialize this chain into xml and return this xml to the calling client.

Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • 1
    Is this an ASMX service, or a WCF service? And, BTW, this won't work if the exception is not serializable. – John Saunders Dec 05 '11 at 17:24
  • Neither, it's a completely custom web service. – Lloyd Dec 05 '11 at 17:36
  • Seems this could be what I'm after - http://stackoverflow.com/questions/486460/how-to-serialize-an-exception-object-in-c – Lloyd Dec 05 '11 at 17:45
  • 1
    possible duplicate of [In C#, how can I serialize System.Exception? (.Net CF 2.0)](http://stackoverflow.com/questions/390051/in-c-how-can-i-serialize-system-exception-net-cf-2-0) – John Saunders Dec 05 '11 at 18:01

1 Answers1

0

I'd go with your own comment to serialize something that captures relevant exception details. This is safe in that it helps insulate the client from exceptions dealing with unknown exception types, and provides you the flexibility to change the level of exception detail, which may be of concern to avoid potential security holes.

To "rethrow" you can then throw a client-side exception that takes this detail as a parameter (kind of like an inner exception).

If you still want to use real exceptions as much as possible, you can create an explicit list of exception types the client supports, or reflect your client assemblies. Thus, you can rethrow the toplevel exception from the server by creating a local one via Activator.CreateInstance. Perhaps you could combine approaches.

Community
  • 1
  • 1
Kit
  • 20,354
  • 4
  • 60
  • 103