Exception Handling in Web Services (4) _ Server

Source: Internet
Author: User
Tags exception handling

This part of the client's exception handling, we'll look at how the client handles the exceptions thrown from the Web service. To illustrate this practice, let's create a new project categoriesserviceclient. Once the project is created, add a command button to the default form and name it btninvoke. Because the Web service needs to be referenced on the client, a webreference is added to the project Categoriesservice. You can complete the Add by project->addreference menu options. Then modify the command button's click event, as shown below.

Privatevoidbtninvoke_click (OBJECTSENDER,SYSTEM.EVENTARGSE)

{

Try

{

Categoriescat=newcategories ();

MessageBox.Show (cat. Addcategories ("Xmlnamespacemanagernsmanager=new

XmlNamespaceManager (Doc. NameTable);

Addthenamespacetothenamespacemanager

Nsmanager.addnamespace ("Errorns",

"Http://tempuri.org/CategoriesService");

Xmlnodecategorynode=

Doc. Documentelement.selectsinglenode ("Errorns:error",

Nsmanager);

Stringerrornumber=

Categorynode.selectsinglenode ("Errorns:errornumber",

Nsmanager). InnerText;

Stringerrormessage=
Categorynode.selectsinglenode ("Errorns:errormessage",

Nsmanager). InnerText;

Stringerrorsource=

Categorynode.selectsinglenode ("Errorns:errorsource",

Nsmanager). InnerText;

MessageBox.Show ("Errornumberis" +errornumber);

MessageBox.Show ("Errormessageis" +errormessage);

MessageBox.Show ("Errorsourceis" +errorsource);

catch (Exceptionex)

{

MessageBox.Show (ex. message);

}

}

The client needs to handle the exceptions generated by the Web service. Because the exceptions generated by Web services are SoapException, the code for the client application that invokes the Web service should be contained in the Try...catch block, and the first catch block should have a processor to capture the SoapException. Let's take a look at the code shown above.

First, create an instance of the Categories class. Then pass in the required XML string parameter and invoke the Addcategories method of the Categories class. After that, we let the catch block handle the exception generated by the Web service. In this code block, we show the creator of the exception as a message balloon. We complete this display by using the code attribute of the SoapException object. If the exception is due to an illegal input from the client, the code attribute is set to client. If the exception occurs because the Web service code (for example, the library server is closed), the Code property is set to server.

We then import the XML data from the detail element contained in the SoapException object into a XmlDocument object. Code similar to Web services, where the namespace is also associated with the XmlDocument object using the XmlNamespaceManager object. The values that are contained in different elements are then taken out and assigned to local variables. Finally, use the message prompt box to display the value of the local variable. Put it together.

Now that we've finished the client application, let's run the test. If you run the client, it displays a message box (with a true value), indicating that the categories details have been successfully saved to the database. Now, delete the element from the input XML data, and run the client program. A message will be given that the exception is due to the client program, and that additional exception information can be obtained in the SoapException object.

As mentioned earlier, if a Web service failure is caused by some server-side problems, the Code property of the SoapException object should be set to server. To test this, modify the connection string in the Web service to an invalid value. Now, if you run the client, you'll get a message that says the exception is the server (the Web service in the example).

Conclusion

In this article, we've learned how to use the SoapException object to handle and pass exceptions to the Web service's clients. We also learned how the SoapException object uses SoapFault encoding (defined in the SOAP specification) to pass the exception. Incidentally, we also discussed the steps to handle the exceptions generated by the client. Although the functionality we create is very simple, it provides a solid foundation for us to understand how to throw and handle exceptions in Web services.
(Source: Pconline)

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.