WCF REST 完整服务中的异常处理

Posted

技术标签:

【中文标题】WCF REST 完整服务中的异常处理【英文标题】:Exception Handling in WCF REST Full Service 【发布时间】:2014-11-22 02:59:42 【问题描述】:

我正在使用 WCF Service 4.5 F/W 开发 REST Full API。请建议如何向客户发送异常或任何业务验证消息。我正在寻找一个非常通用的解决方案,请建议最好的方法。 我尝试了以下一些方法,

    在向客户端发送响应之前设置 OutgoingWebResponseContext。

    定义的故障合约只能正常工作 添加服务引用(代理类)在 REST FULL 环境中不起作用。

    在 catch 块中添加 WebFaultException 类。

    WCF Rest Starter Kit - 我收到了一些关于此的文章和帖子,但他们的 CodePlex 官方网站建议不再提供此功能。 link。所以,我不想这样做。

这些没有按预期工作.. 我的示例代码片段: 接口:

    [OperationContract]
    [FaultContract(typeof(MyExceptionContainer))]
    [WebInvoke(Method = "GET", UriTemplate = "/Multiply/num1/num2", BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    string Multiply(string num1, string num2);

实施:

         public string Multiply(string num1, string num2)
    
        if (num1 == "0" || num2 == "0")
        
            try
            
                MyExceptionContainer exceptionDetails = new MyExceptionContainer();
                exceptionDetails.Messsage = "Business Rule violatuion";
                exceptionDetails.Description = "The numbers should be non zero to perform this operation";
                //OutgoingWebResponseContext outgoingWebResponseContext = WebOperationContext.Current.OutgoingResponse;
                //outgoingWebResponseContext.StatusCode = System.Net.HttpStatusCode.ExpectationFailed;
                //outgoingWebResponseContext.StatusDescription = exceptionDetails.Description.ToString();
               // throw new WebFaultException<MyExceptionContainer>(exceptionDetails,System.Net.HttpStatusCode.Gone);
                throw new FaultException<MyExceptionContainer>(exceptionDetails,new FaultReason("FaultReason is " + exceptionDetails.Messsage + " - " + exceptionDetails.Description));
            
            catch (Exception ex)
            
                throw new FaultException(ex.Message);
            
        

        return Convert.ToString(Int32.Parse(num1) * Int32.Parse(num2));
    

Web.Config:

  <system.serviceModel>
<services>
  <service name="TestService.TestServiceImplementation" behaviorConfiguration="ServiceBehaviour">
    <endpoint binding="webHttpBinding" contract="TestService.ITestService" behaviorConfiguration="webHttp" >
    </endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="webHttp" >
      <webHttp helpEnabled="true" faultExceptionEnabled="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

客户:

try
        
            string serviceUrl = "http://localhost:51775/TestService.cs.svc/Multiply/0/0";
            //Web Client
            //WebClient webClient = new WebClient();
            //string responseString = webClient.DownloadString(serviceUrl);
            WebRequest req = WebRequest.Create(serviceUrl);
            req.Method = "GET";
            req.ContentType = @"application/xml; charset=utf-8";
            HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
            if (resp.StatusCode == HttpStatusCode.OK)
            
                XmlDocument myXMLDocument = new XmlDocument();
                XmlReader myXMLReader = new XmlTextReader(resp.GetResponseStream());
                myXMLDocument.Load(myXMLReader);
                Console.WriteLine(myXMLDocument.InnerText);
            
            Console.ReadKey();
        
        catch (Exception ex)
         
        Console.WriteLine(ex.Message);
        

这是我的第一个问题,感谢所有热情的开发者..!

【问题讨论】:

【参考方案1】:

Web.Config:

<system.serviceModel>
   <services>
    <service name="TestService.TestServiceImplementation">
      <endpoint address=""
                binding="webHttpBinding"
                contract="TestService.Service.ITestServiceImplementation" />
    <service/>
  <services/>
    <behaviors>
      <serviceBehaviors>
        <behavior>         
          <serviceMetadata httpGetEnabled="true"/>         
          <serviceDebug includeExceptionDetailInFaults="true"/>
        <behavior/>
      <serviceBehaviors/>
    <behaviors/>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  <system.serviceModel/>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>   
    <directoryBrowse enabled="true"/>
  <system.webServer/>

实施:

 public string Multiply(string num1, string num2)

    if (num1 == "0" || num2 == "0")
    
        try
        
            MyExceptionContainer exceptionDetails = new MyExceptionContainer();
            exceptionDetails.Messsage = "Business Rule violatuion";
            exceptionDetails.Description = "The numbers should be non zero to perform this operation";
            //OutgoingWebResponseContext outgoingWebResponseContext = WebOperationContext.Current.OutgoingResponse;
            //outgoingWebResponseContext.StatusCode = System.Net.HttpStatusCode.ExpectationFailed;
            //outgoingWebResponseContext.StatusDescription = exceptionDetails.Description.ToString();
           // throw new WebFaultException<MyExceptionContainer>(exceptionDetails,System.Net.HttpStatusCode.Gone);
            throw new FaultException<MyExceptionContainer>(exceptionDetails,new FaultReason("FaultReason is " + exceptionDetails.Messsage + " - " + exceptionDetails.Description));
        
        catch (FaultException ex)
        
            throw new FaultException(ex.Message);
        
    

    return Convert.ToString(Int32.Parse(num1) * Int32.Parse(num2));

【讨论】:

【参考方案2】:

不要使用FaultException,而是使用以下内容:

服务器:

catch (Exception ex)
            
                throw new System.ServiceModel.Web.WebFaultException<string>(ex.ToString(), System.Net.HttpStatusCode.BadRequest);
            

客户:

catch (Exception ex)
            
                var protocolException = ex as ProtocolException;
                var webException = protocolException.InnerException as WebException;
                if (protocolException != null && webException != null)
                
                    var responseStream = webException.Response.GetResponseStream();
                    var error = new StreamReader(webException.Response.GetResponseStream()).ReadToEnd();
                    throw new Exception(error);
                
                else
                    throw new Exception("There is an unexpected error with reading the stream.", ex);

            

【讨论】:

感谢您的回复。我已经尝试过使用 WebFaultException 但没有运气我仍然无法在客户端获得实际的异常 @SJAlex 请参阅我的更新如何捕获和处理异常。还有其他方法可以通过抛出异常,但我是这样做的。 我不得不调用 responseStream.Seek(0, System.IO.SeekOrigin.Begin);由于某种原因,流不在开始位置。【参考方案3】:

在 wcf 中创建真正的 rest 几乎是不可能的,但利用部分 http协议当然是可能的。

要控制服务中的错误消息,您必须提供一个错误处理程序(一个错误处理程序实现),它挂接到 wcf 堆栈并覆盖此处定义的任何错误处理。

这个接口包含两个方法。一个通知 wcf 是否可以处理某个异常(bool HandleError),如果 HandleError 返回 true,则另一个调用。第二种方法 ProvideFault 让您定义在异常情况下如何响应客户端。

您可以链接处理程序以获得更多粒度。由于您正在休息,因此您可能应该使用 httpstatus 代码来告知适用的错误情况。

http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/07/wcf-extensibility-ierrorhandler.aspx

http://www.remondo.net/wcf-global-exception-handling-attribute-and-ierrorhandler/

【讨论】:

以上是关于WCF REST 完整服务中的异常处理的主要内容,如果未能解决你的问题,请参考以下文章

WCF RESTFul服务中的异常处理

从 WCF 4 REST 模板获取调试输出

服务端增加WCF服务全局异常处理机制

WCF:OneWay OperationContract 上的异常处理

WCF异常处理

WCF 异常处理