C # development-Web Service Exception Handling (1)

Source: Internet
Author: User

In actual development, the use of web services has become increasingly popular, especially on the mobile Internet, mobile terminal response and request data are often obtained by connecting to the web service. Web services are usually published on the server or elsewhere. In short, they are separated from the client.
Therefore, the exception handling of client requests to Web Services is different from that of local operations. According to the principle, Web Service transmission parameters are in XML format.
The client requests web services in three situations:
1. The web service does not respond.
2. An exception occurred during the execution of the request data, resulting in an execution error.
3. Get the correct execution result.
The reason why the web service does not respond may be that the network is interrupted or the web service is not started.
Execution exceptions may be SQL command errors, parameter errors, or other program exceptions.
If the Web service does not respond to an exception, it can be captured when the client requests the web service, that is, try {} catch {MessageBox. Show ("Network interrupted. Please try again later! ");}.
The process of program exceptions in Web services can be divided into two situations:
1. define a set of return value standards to directly send error messages to the client.
2. logs are recorded in Web logs and exceptions are captured by the client.
This blog only writes the first case.
Exception throws are mainly used to facilitate debugging or avoid unnecessary exception handling when a program encounters a problem, making the program more rigorous and robust.
There are two types of data operations: Query, add, delete, and modify:
1. Query:
If the query is normal, a normal result is returned, for example, the structure is as follows:

<? XML version = "1.0" encoding = "gb2312"?> <Newdataset> <student> <serial_no> 232 </serial_no> <dept_code> 2029 </dept_code> <dept_name> Institute of Chemical Engineering </dept_name> </student> <serial_no> 232 </serial_no> <dept_code> 2029 </dept_code> <dept_name> Institute of Chemical Engineering </dept_name> </student> </newdataset>

If an exception occurs, as follows:

<? XML version = "1.0" encoding = "gb2312"?> <Newdataset> <exception> <prediction_flag> 1 </prediction_flag> <prediction_value> no query results </prediction_value> <prediction_error> no query results found! </Exception_error> </exception> </newdataset>

2. The returned structure is also defined as XML in the following format:

<? XML version = "1.0" encoding = "gb2312"?> <Newdataset> <exception> <exception_flag> 0 </exception_flag> <exception_value> execution successful </exception_value> <exception_error> </exception> </newdataset>

If an exception occurs, the format is as follows:

<? XML version = "1.0" encoding = "gb2312"?> <Newdataset> <exception> <exception_flag> 2 </exception_flag> <exception_value> operation exception </exception_value> <exception_error> exception. message () </exception_error> </exception> </newdataset>

The above is the return value of the web server, that is, all requests to the Web service will return the XML document. After parsing, check whether the operation is successful and whether there is any exception.
The program Parsing is as follows:

Public static list <student> getstudent (string strxml, out int flag, out string strvalue, out string strerror) {flag = 0; strvalue = ""; strerror = ""; list <student> lststudent = new list <student> (); // XML parsing xmldocument Doc = new xmldocument (); // load the XML string Doc. loadxml (strxml); xmlnodelist xxlist = Doc. getelementsbytagname ("newdataset"); // obtain the set foreach (xmlnode fathernode in xxlist) {xmlnodelist childlist = fathernode. childnodes; foreach (xmlnode xxnode in childlist) // a collection of objects {student = new student (); xmlnodelist childlist2 = xxnode. childnodes; foreach (xmlnode node in childlist2) {string temp = node. name; Switch (temp) {Case "name": // name student. name = node. innertext; break; Case "sex": // gender student. sex = node. innertext; break; Case "date_of_birth": // student of the birth date. date_of_birth = datetime. parse (node. innertext); break; Case "Nation": // student. nation = node. innertext; break; Case "Identity": // identity student. identity = node. innertext; break; Case "exception_flag": // exception flag = int32.parse (node. innertext); break; Case "exception_value": // strvalue = node. innertext; break; Case "exception_error": // detailed exception description strerror = node. innertext; break; default: break;} lststudent. add (student) ;}} return lststudent ;}

 

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.