Web Services Error CallBack

2 posts, 0 answers
  1. Feizal Amlani
    Feizal Amlani avatar
    91 posts
    Member since:
    Feb 2007

    Posted 03 Nov 2008 Link to this post

    Hello,

    I have a function to call a webservice method like so.
    WS_Service.LookUp(myParam, Success, Failure);

    In the WebService call I am throwing a Custom Exception and setting the Message property to a friendly message.  For example, "You have an Invalid ID"

    My Failure Javascript function does get excecuted.  However, when I try to read the Message Property from the exception that was thrown, I have lost the friendly message that I have set in the origional call.  There must be some kind of conversion going on and wondering how to send a Message Property back to the Failure function.

    function Failure(objErr){

        SetFriendlyMessage(objErr.message);
    }

    Thanks.
  2. Kevin Babcock
    Kevin Babcock avatar
    189 posts
    Member since:
    Mar 2007

    Posted 04 Nov 2008 Link to this post

    Hello Feizal,

    In order to retrieve the exception message, you must invoke the object's get_message() function. Alternatively (and not considered a best practice), you can access the message value directly by calling error._message. Here is a simple example:

    1 using System; 
    2  
    3 namespace KevinBabcock.Examples 
    4
    5     public class CustomException : Exception 
    6     { 
    7         public CustomException(string message) 
    8             : base(message) 
    9         { } 
    10     } 
    11
    12  

    1 using System; 
    2 using System.ComponentModel; 
    3 using System.Drawing.Design; 
    4 using System.Web.Script.Services; 
    5 using System.Web.Services; 
    6  
    7 namespace KevinBabcock.Examples 
    8
    9     [WebService(Namespace = "http://telerik.org/")] 
    10     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    11     [ToolboxItem(false)] 
    12     [ScriptService] 
    13     public class TestService : WebService 
    14     { 
    15         [WebMethod] 
    16         public string GetData() 
    17         { 
    18             throw new CustomException("my custom exception"); 
    19         } 
    20     } 
    21
    22  

    1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="KevinBabcock.Examples._Default" %> 
    2  
    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    4  
    5 <html xmlns="http://www.w3.org/1999/xhtml" > 
    6 <head runat="server"
    7     <title>Example - Web Service Error Callback</title> 
    8     <script type="text/javascript"
    9         function invokeService() { 
    10             var service = new KevinBabcock.Examples.TestService(); 
    11             service.GetData(onCompleted, onError); 
    12             return false; 
    13         } 
    14  
    15         function onCompleted(result) { 
    16             alert('Error. We should never reach this code.'); 
    17         } 
    18  
    19         function onError(error) { 
    20             var message = error.get_message(); 
    21             alert(message); 
    22         } 
    23     </script> 
    24 </head> 
    25 <body> 
    26     <form id="form1" runat="server"
    27         <asp:ScriptManager ID="ScriptManager1" runat="server"
    28             <Services> 
    29                 <asp:ServiceReference Path="~/TestService.asmx" /> 
    30             </Services> 
    31         </asp:ScriptManager>     
    32  
    33         <asp:Button ID="Button1" runat="server" 
    34             OnClientClick="return invokeService();" 
    35             Text="Invoke Service" /> 
    36  
    37     </form> 
    38 </body> 
    39 </html> 
    40  

    I hope this helps. Let me know if you have any further questions.

    Sincerely,
    Kevin Babcock
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top