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

本文关键字:信息 捕获异常 抛出 FaultException 客户端 WCF | 更新日期: 2023-09-27 18:15:52

我正在从客户端向WCF服务进行异步调用。服务抛出FaultException异常。当我在客户端"Completed"事件处理程序中捕获异常时,它捕获异常,但丢失有关异常的所有信息。我得到的只是一个泛型异常,带有以下错误消息:"CommunicationException: The remote server returned an error: 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)

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

如果您的服务代码使用了[WebMethod]属性,那么它就不是WCF。它是一个遗留的ASMX服务,不使用FaultException。查看SoapException类,如果你不能切换到使用WCF。