WCF服務建立與拋出強類型SOAP Fault

原創地址:http://www.cnblogs.com/jfzhu/p/4060666.html                 html

轉載請註明出處安全

 

前面的文章《WCF服務的異常消息》中介紹過,若是WCF Service發生異常時,Service會將異常序列化爲SOAP Fault併發送給客戶端。服務器

默認狀況下,出於安全緣由,WCF Service中未處理的異常的詳細信息不會包括在發送給客戶的SOAP Fault裏,你只能看到一個通用的SOAP Fault(「The server was unable to process the request due to an internal error.」)。在調試程序的時候,若是想在SOAP Fault中包含異常的詳細信息,能夠修改服務器的配置文件。併發

<behaviors>
  <serviceBehaviors>
    <behavior name="includeExceptionDetails">
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>


 

SOAP Fault是XML格式,與平臺無關。一般一個SOAP Fault包含如下節點ide

(1) faultcodepost

(2) faultstringui

(3) detailthis

 

Detail節點能夠用來包括自定義XML信息。spa

 

WCF Service在發生異常時應拋出FaultException或FaultException<T>,而不該該拋出.NET Exception,出於如下兩個緣由:調試

(1)未處理的.NET Exception會使服務器與客戶端之間的channel變爲Fault狀態,繼而致使client proxy沒法使用。

(2).NET Exception只能被.NET平臺理解,而FaultException與平臺無關。若是想跨平臺使用,須要使用FaultException。

 

下面仍是以中《WCF服務的異常消息》的例子來分別演示如何拋出與處理FaultException與強類型的FaultException<T>。

 

(一)使用FaultException

IDemoService.cs:

using System.ServiceModel;

namespace WCFDemo 
{    
    [ServiceContract(Name = "IDemoService")] 
    public interface IDemoService 
    { 
        [OperationContract]        
        int Divide(int numerator, int denominator); 
    } 
}

 

DemoService.cs:

using System; 
using System.ServiceModel; 
using System.ServiceModel.Activation;

namespace WCFDemo 
{ 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class DemoService : IDemoService 
    { 
        public int Divide(int numerator, int denominator) 
        { 
            if (denominator == 0) 
            { 
                throw new FaultException("Denominator cannot be ZERO!", new FaultCode("DivideByZeroFault")); 
            } 
            return numerator / denominator;           
        } 
    } 
}

 

client:

private void buttonCalculate_Click(object sender, EventArgs e) 
{ 
    try 
    {                
        textBoxResult.Text = demoServiceClient.Divide(Convert.ToInt32(textBoxNumerator.Text), Convert.ToInt32(textBoxDenominator.Text)).ToString(); 
    } 
    catch (FaultException fault) 
    { 
        MessageBox.Show(fault.Code + " - " + fault.Message); 
    } 
}

 

 

image

image

 

SOAP Fault XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
  <s:Body> 
    <s:Fault> 
      <faultcode>s:DivideByZeroFault</faultcode> 
      <faultstring xml:lang="en-US">Denominator cannot be ZERO!</faultstring> 
    </s:Fault> 
  </s:Body> 
</s:Envelope>

 

 

(二)使用強類型FaultException<T>

(1)建立一個自定義SOAP Fault類

DivideByZeroFault.cs:

using System.Runtime.Serialization;

namespace WCFDemo 
{ 
    [DataContract] 
    public class DivideByZeroFault 
    { 
        [DataMember] 
        public string Error { get; set; }

        [DataMember] 
        public string Detail { get; set; } 
    } 
}

 

(2) 在Service方法上使用FaultContractAttribute來指示哪一個操做能夠使用哪一個Fault

IDemoService.cs:

using System.ServiceModel;

namespace WCFDemo 
{    
    [ServiceContract(Name = "IDemoService")] 
    public interface IDemoService 
    { 
        [FaultContract(typeof(DivideByZeroFault))]         [OperationContract]        
        int Divide(int numerator, int denominator); 
    } 
}

 

DemoService.cs:

using System; 
using System.ServiceModel; 
using System.ServiceModel.Activation;

namespace WCFDemo 
{ 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class DemoService : IDemoService 
    { 
        public int Divide(int numerator, int denominator) 
        { 
            try 
            { 
                return numerator / denominator; 
            } 
            catch (DivideByZeroException ex) 
            { 
                DivideByZeroFault fault = new DivideByZeroFault(); 
                fault.Error = ex.Message; 
                fault.Detail = "Denominator cannot be ZERO!"; 
                throw new FaultException<DivideByZeroFault>(fault); 
            } 
        } 
    } 
}

 

client:

private void buttonCalculate_Click(object sender, EventArgs e) 
{ 
    try 
    {                
        textBoxResult.Text = demoServiceClient.Divide(Convert.ToInt32(textBoxNumerator.Text), Convert.ToInt32(textBoxDenominator.Text)).ToString(); 
    } 
    catch (FaultException<DemoServiceReference.DivideByZeroFault> fault) 
    { 
        MessageBox.Show(fault.Detail.Error + " - " + fault.Detail.Detail); 
    } 
}

 

 

image

image

 

返回的SOAP Fault XML:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
  <s:Body> 
    <s:Fault> 
      <faultcode>s:Client</faultcode> 
      <faultstring xml:lang="en-US">The creator of this fault did not specify a Reason.</faultstring> 
      <detail> 
        <DivideByZeroFault xmlns="http://schemas.datacontract.org/2004/07/WCFDemo" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
          <Detail>Denominator cannot be ZERO!</Detail> 
          <Error>Attempted to divide by zero.</Error> 
        </DivideByZeroFault> 
      </detail> 
    </s:Fault> 
  </s:Body> 
</s:Envelope>
相關文章
相關標籤/搜索