我在服务器上有以下代码:
if (actualRowCount > maxRows)
{
throw new DomainException("OverMaxRowLimitException",
new OverMaxRowLimitException(string.Format(MaxRowsMessage,
actualRowCount, maxRows)));
}
这将创建一个DomainException
属性集的新InnerException
。我已经验证这是在调试器中设置的。
因此定义了自定义异常:
public class OverMaxRowLimitException : Exception
{
public OverMaxRowLimitException(string message)
{
this.Message = message;
}
public new string Message { get; set; }
}
这应该向客户端返回一个明智的错误消息,以便可以告诉用户有太多行。所以在负载完成处理程序中我们希望:
if (result.HasError)
{
ShowError(result.Error.InnerException.Message);
}
不幸的是,InnerException
属性为null
。因此,我们必须检查的是外部异常消息的文本,该消息未正确传输到客户端。
有人建议我需要:
<customErrors mode="Off" />
在网络配置文件中。我试过这个并没有用。
也有人建议我需要:
[KnownType(typeof(OverMaxRowLimitException))]
关于数据合同。现在除非我把它放在错误的地方,否则这也行不通。
我还尝试将[Serializable]
添加到自定义例外,并将其替换为普通Exception
。这些都没有奏效。
那么,为什么InnerException
属性为null。我错过了什么?
答案 0 :(得分:0)
我想你应该使用
[FaultContract(typeof(OverMaxRowLimitException))]
您的运营合同。您的OverMaxRowLimitException
应该是可序列化的。
同样在使用中,我认为抛出异常的正确方法是使用
throw new FaultException<OverMaxRowLimitException>(yourException);