在使用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 mode ="Off" />
</system.runtime.remoting>
本文介绍了如何在Remoting技术中使用自定义异常类,并通过实现序列化接口确保异常可以跨进程传递。自定义的SyncException类展示了如何保存和读取枚举类型的异常信息。

被折叠的 条评论
为什么被折叠?



