Client's abnormal processing
This part, we will see how the client handles the exception 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 on the default form and name BTNInvoke. Since the web service needs to be referenced to the client, add a web reference to the project CategoriesService. You can do it through the Project-> Add Reference menu option. Then modify the Click event of the command button, as shown below.
Private void btninvoke_click (Object Sender, System.Eventargs E)
{
Try
{
Categories cat = new categories ();
Messagebox.show (Cat.Addcategories (" XML Version = '1.0'?>
CategoryDescription> category> categories> "). TOSTRING ());
}
Catch (SOAPEXCEPTION SOAPEX)
{
Messagebox.show (soapex.code.toString ());
// load the detail element of the sootexception Object
XmLDocument Doc = New XmLDocument ();
Doc.LoadXml (SOAPEX.DETAIL.outerxml);
XMLNameSpaceManager nsmanager = new
XMLNameSpaceManager (Doc.nametable);
// add the namespace to the namespacemanager
nsmanager.addnamespace ("Errorns",
"http://tempuri.org/categoriesservice");
XMLNode CategoryNode =
Doc.documentelement.selectsinglenode ("Errorns: Error",
nsmanager);
String ErrorNumber =
CategoryNode.selectsinglenode ("Errorns: ErrorNumber",
nsmanager .innertext;
String ErrorMessage =
CategoryNode.selectsinglenode ("Errorns: ErrorMessage",
nsmanager .innertext;
String Errorsource =
CategoryNode.selectsinglenode ("Errorns: ErrorSource",
nsmanager .innertext;
MessageBox.show ("Error Number IS" ErrorNumber);
MessageBox.show ("Error Message Is" ErrorMessage); MessageBox.Show ("Error Source IS" Errorsource;
}
Catch (Exception EX)
{
Messagebox.show (ex.Message);
}
}
The client needs to handle the exception generated by the web service. Because the exception generated by the web service is in the form of SOAPEXCeption, the code for calling the web service should be included in the try ... catch block, and the first CATCH block should have a processor that captures the SOAPEXCEPTION. Let us take a general look at the code shown above.
First create an instance of a Categories class. Then pass the required XML string parameters to call the categories class's AddCategories method. Once, we let the Catch block handle the exception generated by the web service. In this block, we display an abnormal generator in the way in the message prompt box. We complete this display by using the Code property of the SOAPEXCEPTION object. If the exception is generated because the client's illegal input is generated, the Code property is set to a client. If the exception is generated because the web service code (for example, the library server has been turned off), the Code property is set to Server.
Then, we import XML data contained in the Detail element of the SOAPEXCEPTION object to an XMLDocument object. Similar to the web service code, here is also associated with the namespace with the XMLDocument object using the XMLNameSpaceManager object. Thereafter, the value contained in different elements is taken out and gives them to the local variable. Finally, the value of the local variable is displayed using the message prompt box.
Put it together
At this point, we have completed the client application, and now let us run test it. If you run a client, it will display a message box (with a true value), then the Categories Details have successfully saved to the database. Now, delete the
As mentioned earlier, if the web service failed is caused by some problems of the server, the Code property of the SOAPEXCEPTION object should be set to Server. In order to test this, modify the connection string in the web service is an invalid value. Now, if you run a client, you will get a reason for the exception that is a message of Server (Web service in this example).
in conclusion
In this article, we have learned how to use
SOAPEXCEPTION
Objects to handle and transfer exception
Web
Service client. We also learned
SOAPEXCEPTION
How is the object use?
SOAP FAULT
Code (definition
SOAP
The specification is to deliver an abnormality. Incident, we also discussed the steps of processing an exception generated by the client. Although the function we created is very simple, it gives us understand how to throw and process
Web
The exceptions in the service provide a solid foundation.