【发布时间】: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,但是taggetWCF,是一个还是两个? -
@SeM 抱歉,这只是 WCF。我会更新
标签: c# wcf faultcontract