Workardound non-serializable HttpException
authorAndrew Skiba <andrews@mono-cvs.ximian.com>
Thu, 23 Nov 2006 18:00:10 +0000 (18:00 -0000)
committerAndrew Skiba <andrews@mono-cvs.ximian.com>
Thu, 23 Nov 2006 18:00:10 +0000 (18:00 -0000)
svn path=/trunk/mcs/; revision=68406

mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/MyHost.cs

index 3874b11c0bd7a49a55546fe38aab17d220cdca53..bcea5889e041b5299ca232fc872bc292d0e01a07 100644 (file)
@@ -10,7 +10,9 @@ using System.Web.Configuration;
 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
@@ -45,6 +47,11 @@ namespace MonoTests.SystemWeb.Framework
 \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
@@ -56,6 +63,29 @@ namespace MonoTests.SystemWeb.Framework
                        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