Remoting 错误信息类序列化问题


   在使用Remoting技术时,如果需要使用自己定义错误类即不是简单的使用Exception类时,自定义类除了要从Exception继承外,还要实现序列化接口,才可以使用,下面定义了SyncException类
------

    public enum SyncExceptionTypes { FileNotExists = 0, FileAccessError = 1, AuditError = 2, }

    [Serializable]
    public class SyncException : Exception, ISerializable
    {
        #region 属性
        SyncExceptionTypes _exceptionType;
        public SyncExceptionTypes ExceptionType
        {
            get { return _exceptionType; }
        }
        #endregion

        protected SyncException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this._exceptionType = (SyncExceptionTypes)info.GetValue("ExceptionType", typeof(SyncExceptionTypes));
        }
        public SyncException(string msg, SyncExceptionTypes type)
            : base(msg)
        {
            _exceptionType = type;
        }


        #region ISerializable 成员

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //调用父类的序列化以保存数据
            base.GetObjectData(info, context);
            info.AddValue("ExceptionType", _exceptionType, typeof(SyncExceptionTypes));
        }

        #endregion
    }
--------------
  最后注意在服务端配置文件里做关闭客户端错误
<system.runtime.remoting>
.......................
 <!--
     只有把 customErrors 配置成 Off ,服务器端的详细错误异常,才能传递到客户端
     默认是不传递完整的错误异常的。
     -->

<customErrors mode ="Off" />
  </system.runtime.remoting>

原文地址:https://www.cnblogs.com/wdfrog/p/1214207.html