Client exception handling In this section, we will see how the client handles exceptions thrown from the web service. To illustrate this approach, 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 side, add a WebReference in the project CategoriesService. The addition can be done through the Project->AddReference menu option. Then modify the Click event of the command button as shown below.
privatevoidbtnInvoke_Click(objectsender,)
{
try
{
Categoriescat=newCategories();
((" XmlNamespaceManagernsManager=new
XmlNamespaceManager();
//AddthenamespacetotheNamespaceManager
("errorNS",
"/CategoriesService");
XmlNodecategoryNode=
("errorNS:Error",
nsManager);
stringerrorNumber=
("errorNS:ErrorNumber",
nsManager).InnerText;
stringerrorMessage=
("errorNS:ErrorMessage",
nsManager).InnerText;
stringerrorSource=
("errorNS:ErrorSource",
nsManager).InnerText;
("ErrorNumberis"+errorNumber);
("ErrorMessageis"+errorMessage);
("ErrorSourceis"+errorSource);
} catch(Exceptionex)
{
();
}
}
The client needs to handle exceptions generated by the web service. Because exceptions generated by the web service are in the form of SoapException, the code of the client application calling the web service should be included in the try... catch block, and the first catch block should have a processor that captures 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 call the AddCategories method of the Categories class. Then, we let the catch block handle exceptions generated by the web service. In this code block, we display the producer of the exception in the form of a message prompt box. We complete this display by using the Code property of the SoapException object. If the exception is generated due to illegal input from the client, the Code property is set to Client. If the exception is generated by the web service code (for example, the database server has been shut down), the Code property is set to Server.
Then, we import the XML data contained in the Detail element of the SoapException object into an XmlDocument object. Similar to the code of a web service, the namespace and the XmlDocument object are also used here to associate the namespace with the XmlDocument object. After that, the values contained in the different elements are taken out and assigned to the local variables. Finally, use the message prompt box to display the value of the local variable. Put it together
At this point, we have finished the customer application, and now let's run test it. If you run the client program, it will display a message box (with true value), then it will indicate that the categories details have been saved to the database successfully. Now, remove the element from the input XML data and run the client program. You will get a message that the cause of the exception is the client program. In addition, you can also get more exception information in the SoapException object.
As mentioned earlier, if the web service failure is caused by some server-side problem, 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 program, you will get a message indicating that the cause of the exception is Server (the web service in this example).
in conclusion
In this article, we have learned how to use the SoapException object to handle and pass exceptions to the client of the web service. We also learn how SoapException objects use SOAP fault encoding (defined in the SOAP specification) to pass exceptions. By the way, we also discussed the steps to handle exceptions generated by the client. Although the functions of the application we create are very simple, it provides a solid foundation for us to understand how to throw and handle exceptions in web services.
(Source: PConline)