using System.Collections;\r
using System.Runtime.Remoting.Messaging;\r
using System.Reflection;\r
-using System.Threading;
+using System.Threading;\r
+using System.Runtime.Serialization;\r
+using System.Runtime.Serialization.Formatters.Binary;
\r
namespace MonoTests.SystemWeb.Framework\r
{\r
\r
private static void RethrowException (Exception inner)\r
{\r
+ Exception serializableInner = FindSerializableInner (inner);\r
+ if (serializableInner != inner) {\r
+ throw new Exception ("Cannot serialize exception of type " + inner.GetType ().Name,\r
+ serializableInner);\r
+ }\r
Exception outer;\r
try { //Try create a similar exception and keep the inner intact\r
outer = (Exception) Activator.CreateInstance (inner.GetType (),\r
throw outer;\r
}\r
\r
+ private static Exception FindSerializableInner (Exception inner)\r
+ {\r
+ //FIXME: what can be a less expansive, but equally reliable\r
+ //check that exception can pass remoting?\r
+ Exception ex = inner;\r
+ Exception mostInner = null;\r
+ while (ex != null) {\r
+ try {\r
+ BinaryFormatter f = new BinaryFormatter ();\r
+ f.Serialize (new MemoryStream (), ex);\r
+ //serialization succeeded, return it\r
+ return ex;\r
+ }\r
+ catch (SerializationException) {\r
+ mostInner = ex;\r
+ ex = ex.InnerException;\r
+ }\r
+ }\r
+ //no inner exceptions remain, create one with message and stack of the most inner\r
+ ex = new Exception (mostInner.Message + " Call stack: " + mostInner.StackTrace);\r
+ return ex;\r
+ }\r
+\r
private static HttpWorkerRequest GetMyWorkerRequest ()\r
{\r
IServiceProvider isp = HttpContext.Current as IServiceProvider;\r