views:

1489

answers:

5

Hi All,

We are invoking Asp.Net ajax web service from the client side. So the JavaScript functions have calls like:

// The function to alter the server side state object and set the selected node for the case tree.

function JSMethod(caseId, url)
{  
    Sample.XYZ.Method(param1, param2, OnMethodReturn);  
}

function OnMethodReturn(result)
{
   var sessionExpiry = CheckForSessionExpiry(result);
   var error = CheckForErrors(result);

   ... process result
}

And on the server side in the ".asmx.cs" file: namespace Sample

[ScriptService]
class XYZ : WebService
{
       [WebMethod(EnableSession = true)]
        public string Method(string param1, string param2)
        {
            if (SessionExpired())
            {
                return sessionExpiredMessage;
            }
            .
            .
            .
        }
}

The website is setup to use form based authentication. Now if the session has expired and then the JavaScript function "JSMethod" is invoked, then the following error is obtained: Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'Method' failed with the following error: System.InvalidOperationException-- Authentication failed.

This exception is raised by method "function Sys$Net$WebServiceProxy$invoke" in file "ScriptResource.axd":

function Sys$Net$WebServiceProxy$invoke
{

             .
             .
             .
               {
                    // In debug mode, if no error was registered, display some trace information
                    var error;
                    if (result && errorObj) {
                        // If we got a result, we're likely dealing with an error in the method itself
                        error = result.get_exceptionType() + "-- " + result.get_message();
                    }
                    else {
                        // Otherwise, it's probably a 'top-level' error, in which case we dump the
                        // whole response in the trace
                        error = response.get_responseData();
                    }
                    // DevDiv 89485: throw, not alert()
                    throw Sys.Net.WebServiceProxy._createFailedError(methodName, String.format(Sys.Res.webServiceFailed, methodName, error));
}

So the problem is that the exception is raised even before "Method" is invoked, the exception occurs during the creation of the Web Proxy. Any ideas on how to resolve this problem

A: 

You have a callback method (OnMethodReturn) specified in the WebMethod call, but not an error handler method. You need to create one and pass it into as you do the callback method. Then you can handle your failed WebMethod calls in there.

Kon
A: 

This problem occurs even before the Ajax framework can invoke the target method, it fails while creating the web proxy. Anyway I solved the problem by enabling Anonymous access to the Web Service folder and checking for Session explicitly in the Web Service methods

Ngm
A: 

Why not use, try { } catch { } around your JS method call?

Strelok
A: 

Note try..catch around JS method call does not work, I tried it.

Ngm
A: 

try this one...use "static"

[WebMethod(EnableSession = true)]
public static string Method(string param1, string param2)
cloudlight
-1: Static is only for PageMethods
John Saunders