Create and throw a strong SOAP Fault and wcfsoap

Source: Internet
Author: User

Create and throw a strong SOAP Fault and wcfsoap

Address: http://www.cnblogs.com/jfzhu/p/4060666.html

Reprinted please indicate the source

 

As described in the previous article "exception messages of the WCF Service", if a exception occurs in the WCF Service, the Service serializes the exception to the SOAP Fault and sends it to the client.

By default, for security reasons, details of exceptions not handled in the WCF Service are not included in the SOAP Fault sent to the customer, you can only see one common SOAP Fault ("The server was unable to process the request due to an internal error. "). When debugging a program, you can modify the server configuration file if you want to include detailed exception information in the SOAP Fault.

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


 

SOAP Fault is in XML format and has nothing to do with the platform. Generally, a SOAP Fault contains the following nodes:

(1) faultcode

(2) faultstring

(3) detail

 

The Detail node can be used to include custom XML Information.

 

When an Exception occurs, the WCF Service should throw a FaultException or FaultException <T> instead of A. NET Exception for the following two reasons:

(1) unprocessed. NET Exception will change the channel between the server and the client to the Fault state, and thus the client proxy cannot be used.

(2). NET Exception can only be understood by the. NET platform, and FaultException is irrelevant to the platform. FaultException is required for cross-platform use.

 

The following uses the exception message of the WCF Service as an example to demonstrate how to throw and process FaultException and strong-type FaultException <T>.

 

(1) Use 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);     } }

 

 

<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>

 

 

(2) Use a strong-type FaultException <T>

(1) create a custom SOAP Fault class

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) Use FaultContractAttribute on the Service method to indicate which operation can use the 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);     } }

 

 

<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>
What is a soap fault exception?

On the docs.oracle.com/..n.html, I also learned java, but I forgot about it... Hope you can help me.

Like soap, WCF is a new technology

Silverlight is designed to compete with flash and provide a good user experience. It is easy to make 2D and 3D effects. It can be said that it is a web version of WPF. Soap is not a new technology. WCF is a windows communication foundation, distributed system technology.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.