【问题标题】:Return Exception message in JSON format以 JSON 格式返回异常消息
【发布时间】:2018-10-30 05:49:01
【问题描述】:

我正在开发在生产中运行良好的WCF 应用程序。它有很多服务方法。现在要求如果配置文件中的一个字段设置为true,那么它必须向消费者显示一些自定义消息。

我创建了构造函数以确保每个请求都会调用它并在那里检查该配置键并抛出FaultException,如下所示

public RESTService()
{
    if (ConfigurationManager.AppSettings("BlockLogin") == "1")
        throw new FaultException("Application under maintainance");
}

但它没有在JSON 中给出响应。相反,它以html 格式返回它,如下所示

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Request Error</title>
        <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
    </head>
    <body>
        <div id="content">
            <p class="heading1">Request Error</p>
            <p xmlns="">The server encountered an error processing the request. Please see the 
                <a rel="help-page" href="http://172.16.3.156:81/_RestAPI/RESTService.svc/help">service help page</a> for constructing valid requests to the service.
            </p>
        </div>
    </body>
</html>

【问题讨论】:

  • 我不认为,在当前情况下使用Exception 是一个好主意。没有什么异常发生,您只是想通知用户该应用程序正在维护中。只需创建一个错误响应。
  • @SeM 我该怎么做?建设者的想法正确吗?
  • @Imad 请参考这个social.msdn.microsoft.com/Forums/vstudio/en-US/…>
  • 你在问题​​中说这个项目是WPF,但是tagget WCF,是一个还是两个?
  • @SeM 抱歉,这只是 WCF。我会更新

标签: c# wcf faultcontract


【解决方案1】:

您不需要使用异常 - 正如您所看到的,WCF 会将其转换为 500 服务器错误,而不是让您控制。

您可以创建一个名为(例如)BusinessFault 的类(普通类):

public class BusinessFault
{
 public string ErrorCode { get; set; }
 public string ErrorMessage { get; set; }
}

那么,当你想返回一个故障时:

if (ConfigurationManager.AppSettings("BlockLogin") == "1")
{
  return new BusinessFault() {
    ErrorCode = "0001",
    ErrorMessage = "Application under maintainance"
  }
}

【讨论】:

  • 然后像往常一样将对象返回给消费者。请注意,这仍然返回 HTTP 错误代码 200。
  • 感谢您的回答。我能否以一种在请求服务时自动调用它的方式实现它。如果可能的话,我想保持所有操作不变。
猜你喜欢
  • 1970-01-01
  • 2020-06-12
  • 2015-08-31
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多