GetObjectData() private method

private GetObjectData ( SerializationInfo info, StreamingContext context ) : void
info SerializationInfo
context StreamingContext
return void
Exemplo n.º 1
1
        private void PreserveStackTrace(Exception exception)
        {
            var context = new StreamingContext(StreamingContextStates.CrossAppDomain);
            var serializationInfo = new SerializationInfo(typeof(Exception), new FormatterConverter());
            var constructor = typeof(Exception).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);

            exception.GetObjectData(serializationInfo, context);
            constructor.Invoke(exception, new object[] { serializationInfo, context });
        }
        /// <remarks>
        /// Credit to MvcContrib.TestHelper.AssertionException for PreserveStackTrace
        /// </remarks>
        private static void PreserveStackTrace(Exception e)
        {
            var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
            var mgr = new ObjectManager(null, ctx);
            var si = new SerializationInfo(e.GetType(), new FormatterConverter());

            e.GetObjectData(si, ctx);
            mgr.RegisterObject(e, 1, si);
            mgr.DoFixups();
        }
Exemplo n.º 3
0
        private void PreserveStackTrace(Exception exception)
        {
            var context = new StreamingContext(StreamingContextStates.CrossAppDomain);
            var objectManager = new ObjectManager(null, context);
            var serializationInfo = new SerializationInfo(exception.GetType(), new FormatterConverter());

            exception.GetObjectData(serializationInfo, context);
            objectManager.RegisterObject(exception, 1, serializationInfo);
            objectManager.DoFixups();
        }
Exemplo n.º 4
0
        public static void PreserveStackTrace(Exception e)
        {
            var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
            var mgr = new ObjectManager(null, ctx);
            var si = new SerializationInfo(e.GetType(), new FormatterConverter());

            e.GetObjectData(si, ctx);
            mgr.RegisterObject(e, 1, si); // prepare for SetObjectData
            mgr.DoFixups(); // ObjectManager calls SetObjectData

            // voila, e is unmodified save for _remoteStackTraceString
        }
Exemplo n.º 5
0
 static public int GetObjectData(IntPtr l)
 {
     try {
         System.Exception self = (System.Exception)checkSelf(l);
         System.Runtime.Serialization.SerializationInfo a1;
         checkType(l, 2, out a1);
         System.Runtime.Serialization.StreamingContext a2;
         checkValueType(l, 3, out a2);
         self.GetObjectData(a1, a2);
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
		private JsonObject ToExceptionJsonObject(Exception e, int depth)
		{
			var o = new JsonObject();
#if !DOTNETCORE
			var si = new SerializationInfo(e.GetType(), new FormatterConverter());
			var sc = new StreamingContext();
			e.GetObjectData(si, sc);
			//TODO Loop over ISerializable data

			var helpUrl = si.GetString("HelpURL");
			var stackTrace = si.GetString("StackTraceString");
			var remoteStackTrace = si.GetString("RemoteStackTraceString");
			var remoteStackIndex = si.GetInt32("RemoteStackIndex");
			var exceptionMethod = si.GetString("ExceptionMethod");
			var hresult = si.GetInt32("HResult");
			var source = si.GetString("Source");
			var className = si.GetString("ClassName");
#else
			var helpUrl = e.HelpLink;
			var stackTrace = e.StackTrace;
			var remoteStackTrace = string.Empty;
			var remoteStackIndex = string.Empty;
			var exceptionMethod = string.Empty;
			var hresult = e.HResult;
			var source = e.Source;
			var className = string.Empty;
#endif

			o.Add("Depth", depth);
			o.Add("ClassName", className);
			o.Add("Message", e.Message);
			o.Add("Source", source);
			o.Add("StackTraceString", stackTrace);
			o.Add("RemoteStackTraceString", remoteStackTrace);
			o.Add("RemoteStackIndex", remoteStackIndex);
			o.Add("HResult", hresult);
			o.Add("HelpURL", helpUrl);
#if !DOTNETCORE
			this.WriteStructuredExceptionMethod(o, exceptionMethod);
#endif
			return o;
		}
Exemplo n.º 7
0
        private static void InternalPreserveStackTrace(Exception e)
        {
            // check if method is applicable (exception type should have the deserialization constructor)
            var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            var constructor = e.GetType().GetConstructor(bindingFlags, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
            if (constructor == null)
            {
                return;
            }

            var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
            var mgr = new ObjectManager(null, ctx);
            var si = new SerializationInfo(e.GetType(), new FormatterConverter());

            e.GetObjectData(si, ctx);
            mgr.RegisterObject(e, 1, si); // prepare for SetObjectData
            mgr.DoFixups(); // ObjectManager calls the deserialization constructor

            // voila, e is unmodified save for _remoteStackTraceString
        }
		public void GetObjectData_Null ()
		{
			Exception e = new Exception ();
			e.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
		}
Exemplo n.º 9
0
        /// <summary>
        ///   Constructs the exception details for the server to return
        /// </summary>
        public static RpcExceptionInfo Create(Exception error, RpcErrorDetailBehavior details)
        {
            if (details == RpcErrorDetailBehavior.NoDetails)
            {
                return DefaultInstance;
            }

            Builder builder = CreateBuilder();
            SerializationInfo si = new SerializationInfo(error.GetType(), FormatterConverter);

            if (details == RpcErrorDetailBehavior.MessageOnly)
            {
                si.AddValue("Message", error.Message);
            }
            else if (details == RpcErrorDetailBehavior.FullDetails)
            {
                try
                {
                    error.GetObjectData(si, StreamingContext);
                    builder.SetHasFullDetails(true);
                }
                catch
                {
                }
            }

            builder.AssemblyName = si.AssemblyName;
            builder.FullTypeName = si.FullTypeName;

            foreach (SerializationEntry se in si)
            {
                switch (se.Name)
                {
                    case "ClassName":
                        if (se.Value is string)
                        {
                            builder.SetClassName((string) se.Value);
                        }
                        break;
                    case "Message":
                        if (se.Value is string)
                        {
                            builder.SetMessage((string) se.Value);
                        }
                        break;
                        //case "Data": if (se.Value is string) builder.SetData((string)se.Value); break;
                    case "InnerException":
                        if (se.Value is Exception)
                        {
                            builder.SetInnerException(Create((Exception) se.Value, details));
                        }
                        break;
                    case "HelpURL":
                        if (se.Value is string)
                        {
                            builder.SetHelpUrl((string) se.Value);
                        }
                        break;
                    case "StackTraceString":
                        if (se.Value is string)
                        {
                            builder.SetStackTraceString((string) se.Value);
                        }
                        break;
                    case "RemoteStackTraceString":
                        if (se.Value is string)
                        {
                            builder.SetRemoteStackTraceString((string) se.Value);
                        }
                        break;
                    case "RemoteStackIndex":
                        if (se.Value is int)
                        {
                            builder.SetRemoteStackIndex((int) se.Value);
                        }
                        break;
                    case "ExceptionMethod":
                        if (se.Value is string)
                        {
                            builder.SetExceptionMethod((string) se.Value);
                        }
                        break;
                    case "HResult":
                        if (se.Value is int)
                        {
                            builder.SetHResult((int) se.Value);
                        }
                        break;
                    case "Source":
                        if (se.Value is string)
                        {
                            builder.SetSource((string) se.Value);
                        }
                        break;
                    default:
                        {
                            if (se.ObjectType == typeof (String) || //se.ObjectType == typeof(byte[]) || 
                                (se.ObjectType.IsPrimitive && se.ObjectType.Assembly == typeof (String).Assembly))
                            {
                                Types.RpcExceptionData.Builder data = Types.RpcExceptionData.CreateBuilder()
                                    .SetMember(se.Name)
                                    .SetType(se.ObjectType.FullName)
                                    ;
                                if (se.Value != null)
                                {
                                    data.SetValue(se.Value is byte[]
                                                      ? Convert.ToBase64String((byte[]) se.Value)
                                                      : Convert.ToString(se.Value));
                                }
                                builder.AddExceptionData(data.Build());
                            }
                            else
                            {
                                builder.AddExceptionData(
                                    Types.RpcExceptionData.CreateBuilder().SetMember(se.Name).SetType(
                                        typeof (Object).FullName));
                            }
                            break;
                        }
                }
            }

            return builder.Build();
        }
        private void WriteExceptionSerializationInfo(Exception exception, ref string delim, TextWriter output, int depth)
        {
            var si = new SerializationInfo(exception.GetType(), new FormatterConverter());
            var sc = new StreamingContext();
            exception.GetObjectData(si, sc);

            var helpUrl = si.GetString("HelpURL");
            var stackTrace = si.GetString("StackTraceString");
            var remoteStackTrace = si.GetString("RemoteStackTraceString");
            var remoteStackIndex = si.GetInt32("RemoteStackIndex");
            var exceptionMethod = si.GetString("ExceptionMethod");
            var hresult = si.GetInt32("HResult");
            var source = si.GetString("Source");
            var className = si.GetString("ClassName");
            var watsonBuckets = si.GetValue("WatsonBuckets", typeof(byte[])) as byte[];

            //TODO Loop over ISerializable data

            output.Write(delim);
            output.Write("{");
            delim = "";
            this.WriteJsonProperty("Depth", depth, ref delim, output);
            this.WriteJsonProperty("ClassName", className, ref delim, output);
            this.WriteJsonProperty("Message", exception.Message, ref delim, output);
            this.WriteJsonProperty("Source", source, ref delim, output);
            this.WriteJsonProperty("StackTraceString", stackTrace, ref delim, output);
            this.WriteJsonProperty("RemoteStackTraceString", remoteStackTrace, ref delim, output);
            this.WriteJsonProperty("RemoteStackIndex", remoteStackIndex, ref delim, output);
            this.WriteStructuredExceptionMethod(exceptionMethod, ref delim, output);
            this.WriteJsonProperty("HResult", hresult, ref delim, output);
            this.WriteJsonProperty("HelpURL", helpUrl, ref delim, output);

            //writing byte[] will fall back to serializer and they differ in output
            //JsonNET assumes string, simplejson writes array of numerics.
            //Skip for now
            //this.WriteJsonProperty("WatsonBuckets", watsonBuckets, ref delim, output);

            output.Write("}");
            delim = ",";
            if (exception.InnerException != null && depth < 20)
                this.WriteExceptionSerializationInfo(exception.InnerException, ref delim, output, ++depth);
        }
Exemplo n.º 11
0
		public void GetObjectData_Info_Null ()
		{
			Exception e = new Exception ();
			try {
				e.GetObjectData (null, new StreamingContext ());
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("info", ex.ParamName, "#5");
			}
		}
Exemplo n.º 12
0
		public void GetObjectData ()
		{
			string msg = "MESSAGE";
			Exception inner = new ArgumentException ("whatever");
			SerializationInfo si;
			Exception se;

			se = new Exception (msg, inner);
			si = new SerializationInfo (typeof (Exception),
				new FormatterConverter ());
			se.GetObjectData (si, new StreamingContext ());
			Assert.AreEqual (11, si.MemberCount, "#A1");
			Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#A2");
			Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3");
			Assert.AreSame (msg, si.GetString ("Message"), "#A4");
			Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5");
			Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6");
			Assert.IsNull (si.GetString ("StackTraceString"), "#A7");
			Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8");
			Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9");
			Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10");
			Assert.IsNull (si.GetString ("Source"), "#A11");
			Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12");

			// attempt initialization of lazy init members
			Assert.IsNotNull (se.Data);
			Assert.IsNull (se.Source);
			Assert.IsNull (se.StackTrace);

			si = new SerializationInfo (typeof (Exception),
				new FormatterConverter ());
			se.GetObjectData (si, new StreamingContext ());
			Assert.AreEqual (11, si.MemberCount, "#B1");
			Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#B2");
			Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3");
			Assert.AreSame (msg, si.GetString ("Message"), "#B4");
			Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5");
			Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6");
			Assert.IsNull (si.GetString ("StackTraceString"), "#B7");
			Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8");
			Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9");
			Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10");
			Assert.IsNull (si.GetString ("Source"), "#B11");
			Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12");

			try {
				throw new Exception (msg, inner);
			} catch (Exception ex) {
				si = new SerializationInfo (typeof (Exception),
					new FormatterConverter ());
				ex.GetObjectData (si, new StreamingContext ());
				Assert.AreEqual (11, si.MemberCount, "#C1");
				Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#C2");
				Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3");
				Assert.AreSame (msg, si.GetString ("Message"), "#C4");
				Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5");
				Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6");
				Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7");
				Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8");
				Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9");
				Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10");
				Assert.IsNotNull (si.GetString ("Source"), "#C11");
				//Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12");
			}

			try {
				throw new Exception (msg, inner);
			} catch (Exception ex) {
				// force initialization of lazy init members
				Assert.IsNotNull (ex.Data);
				Assert.IsNotNull (ex.StackTrace);

				si = new SerializationInfo (typeof (Exception),
					new FormatterConverter ());
				ex.GetObjectData (si, new StreamingContext ());
				Assert.AreEqual (11, si.MemberCount, "#D1");
				Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#D2");
				Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3");
				Assert.AreSame (msg, si.GetString ("Message"), "#D4");
				Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5");
				Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6");
				Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7");
				Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8");
				Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9");
				Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10");
				Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11");
				//Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12");
			}
		}
Exemplo n.º 13
0
        /// <summary>
        /// Causes the original strack trace of the exception to be preserved when it is rethrown
        /// </summary>
        /// <param name="ex"></param>
		private static void PrepareExceptionForRethrow(Exception ex)
		{
            var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
            var mgr = new ObjectManager(null, ctx);
            var si = new SerializationInfo(ex.GetType(), new FormatterConverter());

            ex.GetObjectData(si, ctx);
            mgr.RegisterObject(ex, 1, si); // prepare for SetObjectData
            mgr.DoFixups(); // ObjectManager calls SetObjectData
		}
        /// <summary>
        /// Writes the properties of a single exception, without inner exceptions
        /// Callers are expected to open and close the json object themselves.
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="delim"></param>
        /// <param name="output"></param>
        /// <param name="depth"></param>
        protected void WriteSingleException(Exception exception, ref string delim, TextWriter output, int depth)
        {
            #if NO_SERIALIZATION
            var helpUrl = exception.HelpLink;
            var stackTrace = exception.StackTrace;
            var remoteStackTrace = string.Empty;
            var remoteStackIndex = -1;
            var exceptionMethod = string.Empty;
            var hresult = exception.HResult;
            var source = exception.Source;
            var className = string.Empty;

            #else
            var si = new SerializationInfo(exception.GetType(), new FormatterConverter());
            var sc = new StreamingContext();
            exception.GetObjectData(si, sc);

            var helpUrl = si.GetString("HelpURL");
            var stackTrace = si.GetString("StackTraceString");
            var remoteStackTrace = si.GetString("RemoteStackTraceString");
            var remoteStackIndex = si.GetInt32("RemoteStackIndex");
            var exceptionMethod = si.GetString("ExceptionMethod");
            var hresult = si.GetInt32("HResult");
            var source = si.GetString("Source");
            var className = si.GetString("ClassName");
            #endif

            //TODO Loop over ISerializable data

            this.WriteJsonProperty("Depth", depth, ref delim, output);
            this.WriteJsonProperty("ClassName", className, ref delim, output);
            this.WriteJsonProperty("Message", exception.Message, ref delim, output);
            this.WriteJsonProperty("Source", source, ref delim, output);
            this.WriteJsonProperty("StackTraceString", stackTrace, ref delim, output);
            this.WriteJsonProperty("RemoteStackTraceString", remoteStackTrace, ref delim, output);
            this.WriteJsonProperty("RemoteStackIndex", remoteStackIndex, ref delim, output);
            this.WriteStructuredExceptionMethod(exceptionMethod, ref delim, output);
            this.WriteJsonProperty("HResult", hresult, ref delim, output);
            this.WriteJsonProperty("HelpURL", helpUrl, ref delim, output);

            //writing byte[] will fall back to serializer and they differ in output
            //JsonNET assumes string, simplejson writes array of numerics.
            //Skip for now
            //this.WriteJsonProperty("WatsonBuckets", watsonBuckets, ref delim, output);
        }