无法从Fault Exceptions(javascript)获取信息

时间:2011-07-03 15:24:24

标签: javascript ajax wcf exception-handling

到目前为止,我已经使用了一个我从javascript调用的Web服务ASMX。它一直工作正常,但因为我需要对序列化进行一些控制,我试图切换到DataContracts和WCF。 但是,我确实认为我有一些误解,因为我试图从客户端代码(javascript)中捕获错误,但我收到的唯一错误代码是: (翻译,所以可能不准确)

“由于实际错误,服务器无法处理请求。您可以通过激活IncludeExceptionDetailInFaults来获取有关错误的更多信息......”

我试图将IncludeExceptionDetailInFauls设置为true,它提供了一些信息,但据我所知,抛出FaultExceptions的重点是隐藏用户的异常,只抛出一些信息?我尝试使用IErrorHandler,但我有 创建了一个更简单的例子:

SVC:

[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";
    }
}

包含在scriptmanager中,并由以下javascript调用:

<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来获取异常信息?

1 个答案:

答案 0 :(得分:2)

有用的资源:

主要步骤:

  1. 通过阻止IErrorHandler接口
  2. 创建自己的错误处理程序
  3. 创建WebHttpBehavior的子类并使用您自己的错误处理程序替换默认错误处理程序
  4. 使用自定义ServiceHostFactory插入自定义/扩展行为或在web.config中定义为behaviorExtension
相关问题