【问题标题】:Return custom error message when includeExceptionDetailInFaults="false"当 includeExceptionDetailInFaults="false" 时返回自定义错误消息
【发布时间】:2014-03-21 11:31:54
【问题描述】:

我在 .Net 框架 4.5 中使用 .net svc 服务,出于生产目的,我必须在我的 web.config 文件中将 includeExceptionDetailInFaults 设置为 false

但有时我需要让我的服务返回自定义错误。

我尝试使用System.ServiceModel.Dispatcher.IErrorHandler.ProvideFault 方法创建自定义FaultException 没有结果,返回的错误消息始终是通用消息。

有可能吗?如果是怎么办??

谢谢

【问题讨论】:

    标签: c# .net exception svc


    【解决方案1】:

    是的,可以返回自定义错误消息。 只需将您的消息包装在 FaultException 中即可:

    try
    {
       // some calls go here
    }
    catch (Exception ex)
    {
       throw new FaultException(ex.Message);
    }
    

    当发生错误时,调用您的服务的客户端会看到一条清晰的消息而不是异常详细信息。

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 2018-01-09
      • 2011-02-24
      • 1970-01-01
      • 2011-08-28
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多