到目前为止,我已经使用了一个我从javascript调用的Web服务ASMX。它一直工作正常,但因为我需要对序列化进行一些控制,我试图切换到DataContracts和WCF。 但是,我确实认为我有一些误解,因为我试图从客户端代码(javascript)中捕获错误,但我收到的唯一错误代码是: (翻译,所以可能不准确)
“由于实际错误,服务器无法处理请求。您可以通过激活IncludeExceptionDetailInFaults来获取有关错误的更多信息......”
我试图将IncludeExceptionDetailInFauls设置为true,它提供了一些信息,但据我所知,抛出FaultExceptions的重点是隐藏用户的异常,只抛出一些信息?我尝试使用IErrorHandler,但我有 创建了一个更简单的例子:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WcfTest
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public string DoOk()
{
// Add your operation implementation here
return "Success";
}
[OperationContract]
public string DoFail()
{
FaultReason reason = new FaultReason("Fail <- This is good :)");
throw new FaultException(reason);
return "Success";
}
}
<a href="java script:void(0);" onclick="DoOk()">OK</a><br />
<a href="java script:void(0);" onclick="DoFail()">Fail</a>
<script>
function DoOk() {
WcfTest.DoOk(
function (result) {
alert("DoOk Success: "+result);
},
function (result) {
alert("DoOk Failed: " + result);
})
}
function DoFail() {
WcfTest.DoFail(
function (result) {
alert("DoFail Success: " + result);
},
function (result) {
alert("DoFail Failed: " + result);
})
}
</script>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WcfTestAspNetAjaxBehavior">
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WcfTestAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="WcfTest">
<endpoint address="" behaviorConfiguration="WcfTestAspNetAjaxBehavior"
binding="webHttpBinding" contract="WcfTest" />
</service>
</services>
</system.serviceModel>
我还尝试使用以下组合标记DoFail方法:
[FaultContract(typeof(FaultException))]
[FaultContract(typeof(FaultReason))]
我误解了什么吗?您是否能够通过抛出FaultException
而无需将IncludeExceptionDetailInFaults
设置为true来获取异常信息?
答案 0 :(得分:2)