Using SoapException in WebService to handle errors in business process execution

Source: Internet
Author: User
Tags empty error code error handling execution header
web| Error | Executes the method of calling WebService in the client to process the business process, if the business handler returns an error, generally we can return the processing result to the customer segment in the way of returning the error code, and the customer segment will do the corresponding error handling for different error codes. In this process, sometimes the client may need a lot of repetitive code to respond to different error codes. A simple example is, for different error codes, display different error messages, in some systems, business processing error messages may be placed in the business database, in which case the customer segment to display error messages, You must also invoke the appropriate service to get the error message.
WebService uses SoapException's detail to provide the ability to return more detailed error information or business-related information to clients, such as using the following code to return an exception to a customer segment in a certified example, and to inform the customer of error codes and 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 generates an exception. \ n" +ex. message));
}
Check return code
if (iret!=0)
{
throw new SoapException (string. Empty, Soapexception.clientfaultcode, "Service", Errorprocess.createsoapexdetail (IRet, ""));
}

return iRet;
}

The code for generating the SoapException detail is as follows:
public static System.Xml.XmlNode createsoapexdetail (int excode,string strmessage)
{
XmlDocument doc = new System.Xml.XmlDocument ();
XmlNode node = doc. CreateNode (XmlNodeType.Element,
SoapException.DetailElementName.Name,
SoapException.DetailElementName.Namespace);


Add error code
System.Xml.XmlNode Nodecode = doc. CreateNode (XmlNodeType.Element, "Excode", "Excode");
Nodecode.innertext = Excode.tostring ();

Increase error messages
System.Xml.XmlNode nodemessage = doc. CreateNode (XmlNodeType.Element, "Exmessage", "exmessage");
if (Strmessage.trim () = "")
{
Nodemessage.innertext = GetErrorMessage (Excode);//According to the appropriate error message from the database
}
Else
{
Nodemessage.innertext = strmessage;
}

Append the two child elements to the detail node.
Node. AppendChild (Nodecode);
Node. AppendChild (Nodemessage);
return node;
}

After the client has received the SoapException processing method is as follows:
catch (SoapException Soapex)
{
IRet = Convert.ToInt32 (soapex.detail["Excode"). InnerText);
MessageBox.Show (soapex.detail["Exmessage"). InnerText, "service-side error", MessageBoxButtons.OK, Messageboxicon.error);

}





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.