-1

我正在从客户端向 WCF 服务进行异步调用。该服务正在引发 FaultException 异常。当我在客户端“已完成”事件处理程序中捕获异常时,它会捕获异常,但会丢失有关它的所有信息。我得到的只是一个带有此错误消息的通用类型异常:“CommunicationException:远程服务器返回错误:NotFound”)。我将 includeExceptionDetailInFaults 设置为 true。

为什么我无法捕捉到 FaultException?

谢谢您的帮助。

以下是相关代码:

WCF 服务代码

  [WebMethod]
    [FaultContract(typeof(DivideByZeroException))]
    public int CountResults(FilterArgs args)
    {
      ...
     DivideByZeroException divByZero = new DivideByZeroException();
     throw new FaultException<DivideByZeroException>(divByZero);

客户端代码

    void seasClient_CountResultsCompleted(object sender, CountResultsCompletedEventArgs e)
    {
        try
        {
          ...
        }
        catch (FaultException ex)
        {
            MessageBox.Show("FaultException" + ex.Message);
        }
        catch (TimeoutException ex)
        {
            MessageBox.Show("TimeoutException" + ex.Message);
        }
        catch (CommunicationException ex)
        {
            MessageBox.Show("CommunicationException" + ex.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception" + ex.Message);
        }

和我的 e.Error.ToString() 消息:

System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
   --- End of inner exception stack trace ---
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at WebAnalysis.SeasService.SeasServiceSoapClient.SeasServiceSoapClientChannel.EndCountResults(IAsyncResult result)
   at WebAnalysis.SeasService.SeasServiceSoapClient.WebAnalysis.SeasService.SeasServiceSoap.EndCountResults(IAsyncResult result)
   at WebAnalysis.SeasService.SeasServiceSoapClient.EndCountResults(IAsyncResult result)
   at WebAnalysis.SeasService.SeasServiceSoapClient.OnEndCountResults(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
4

1 回答 1

1

如果使用该[WebMethod]属性,您的服务代码不是 WCF。这是一个遗留的 ASMX 服务,它不使用FaultException. SoapException如果您无法切换到使用 WCF,请查看课程。

于 2013-05-28T23:51:34.623 回答