WCF抛出FaultException,客户端捕获Exception但丢失所有信息

时间:2013-05-28 23:11:55

标签: c# wcf

我正在从客户端到WCF服务进行异步调用。该服务正在抛出FaultException异常。当我在我的客户端“已完成”事件处理程序中捕获异常时,它会捕获异常,但会丢失有关它的所有信息。我得到的是一个泛型类型Exception,带有以下错误消息:“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)

1 个答案:

答案 0 :(得分:1)

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