Troubleshooting of Webservice errors in asp.net Environment

Source: Internet
Author: User

In Asp.net Ajax technology, we often use scripts on the client to call the WEB service. When a service error occurs (timeout, Division by zero), how can we notify the client, how does the client prompt the user based on the obtained information?

The method is actually very simple. Let's look at the following code.

1. Create a service:

[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ScriptService] // The ScriptService method is used here, so that the client can use scripts to access this service.
Public class ErrorHand: System. Web. Services. WebService

{

[WebMethod] // defines a division function. Note that B cannot be 0 (of course, the input B must be 0 in the demonstration to see how to handle the error)
Public int Division (int a, int B) {return a/B ;}

}

2. perform the following operations on the newly created aspx page:

<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Services> // note that you need to add a web service reference so that you can use the service function on the page.
<Asp: ServiceReference Path = "~ /ErrorHand. asmx "/>
</Services>
</Asp: ScriptManager>
<Input type = "button" value = "Click me" onclick = "division (8, 0)"/> // insert a button to trigger the division function, the following describes how the division function of the script calls the Division service function of the server. (8 divided by 0)
<Script type = "text/javascript" language = "javascript"> // define a script
Function division (a, B) {ErrorHand. division (a, B, getSucceded, failed);} // You Can See ErrorHang, which is the WEB service class we defined. It contains the Division function, which is the service function defined in step 1. Failed is a callback function with errors, that is, a Division error is a function to be executed.
Function failed (error) // executes the callback function. The error parameter is the WebServiceError object returned when an error occurs in the execution of the Division function.

{
Var message = String. format ("TimeOut: {0} \ nMessage: {1} \ nExceptionType: {2} \ nStackTrace: {3}", error. get_timedOut (), error. get_message (), error. get_exceptionType (), error. get_stackTrace (); // Display error information in formatting. You should be familiar with the format method here. The built-in methods of the error object can be read in English, so we will not repeat them here.
Alert (message); // display information.
}
Function getSucceded (result)

{// If it is correct, the result of the call from the service is displayed.

Alert (result );
}
</Script>

Through the simple example above, I discussed the handling methods for errors in calling web Services in the asp.net client, hoping to help you.

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.