Use SoapException in WebService to handle service process execution errors

Source: Internet
Author: User

The WebService method is called on the client to process the business process. If the business processing program returns an error, we can generally return the processing result to the customer segment by returning the error code, the customer segment carries out corresponding error processing for different error codes. According to this process, sometimes the client may need a lot of repeated code to respond to different error codes. A simple example is, different error messages are displayed for different error codes. In some systems, the error messages processed by the business may be stored in the business database. In this case, the customer segment must display the error message, you must also call the corresponding Service to obtain the error message.
WebService uses the Detail of SoapException to provide the client with the ability to return more detailed error information or business-related information. For example, in an authentication example, the following code can be used to return an exception to the client segment, inform the customer of the error codes, error messages, and other relevant information.
Private static int Authenticate (CredentialSoapHeader header)
{
Int iRet;
Try
{
SqlConnection conn = new SqlConnection (CommonDB. ConnectionString );
SqlCommand cmd = new SqlCommand ("P_Employee_Check", conn );
Cmd. Parameters. Add ("@ EmployeeCode", header. Username );
Cmd. Parameters. Add ("@ EmployeePassword", header. Password );
Cmd. Parameters. Add ("@ ReturnValue",-1 );
Cmd. Parameters ["@ ReturnValue"]. Direction = ParameterDirection. ReturnValue;
Cmd. CommandType = CommandType. StoredProcedure;
Conn. Open ();
Cmd. ExecuteNonQuery ();
Conn. Close ();
IRet = Convert. ToInt32 (cmd. Parameters ["@ ReturnValue"]. Value );
}
Catch (Exception ex)
{
IRet = 1000;
Throw new SoapException (string. Empty, SoapException. ServerFaultCode, "Service", ErrorMessage. CreateSoapExDetail (iRet, "database operation exception .." + ex. Message ));

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.