gpt4 book ai didi

c# - 为什么我的 InnerException 没有发送给客户端?

转载 作者:太空狗 更新时间:2023-10-29 19:48:01 26 4
gpt4 key购买 nike

我在服务器上有以下代码:

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))]

关于数据契约(Contract)。现在,除非我把它放在错误的地方,否则这也不起作用。

我还尝试将 [Serializable] 添加到自定义异常并将其替换为通用 Exception。这些都不起作用。

那么,为什么 InnerException 属性为 null。我错过了什么?

最佳答案

我想你应该使用

[FaultContract(typeof(OverMaxRowLimitException))]

为您的操作契约(Contract)。还有你的OverMaxRowLimitException应该是可序列化的。

同样在用法中,我想抛出异常的正确方法是使用

throw new FaultException<OverMaxRowLimitException>(yourException);

关于c# - 为什么我的 InnerException 没有发送给客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14563690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com