2008-12-15 99 views
2

我们从客户端调用Asp.Net ajax Web服务。所以JavaScript函数的调用如下所示:如何解决:在Ajax WebService中会话超时时InvalidOperationException调用

//该函数用于更改服务器端状态对象并为案例树设置所选节点。

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

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

    ... process result 
} 

而就在“.asmx.cs”文件服务器端: 命名空间样品

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

该网站设置为使用基于表单的身份验证。现在,如果会话已过期,然后调用了JavaScript函数“JSMethod”,则 将获得以下错误: Microsoft JScript运行时错误:Sys.Net.WebServiceFailedException:服务器方法'Method'失败,并显示以下错误:System .InvalidOperationException - 身份验证失败。

此异常的方法“功能SYS $ NET $ WebServiceProxy $调用”文件“的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)); 
} 

所以问题是,引发异常,甚至之前的“法”是调用时,在创建Web代理期间发生异常。有关如何解决此问题的任何想法

回答

0

您有一个WebMethod调用中指定的回调方法(OnMethodReturn),但不是错误处理程序方法。您需要创建一个并将其传递到回调方法中。然后你可以在那里处理失败的WebMethod调用。

0

甚至在Ajax框架可以调用目标方法之前发生此问题,它在创建Web代理时失败。无论如何,我通过启用匿名访问Web服务文件夹和明确检查Web服务方法中的会话解决了这个问题

0

为什么不使用,围绕你的JS方法调用try { } catch { }

-1

试试这个...使用 “静态”

[WebMethod(EnableSession = true)] 
public static string Method(string param1, string param2) 
+0

-1:静态仅供PageMethods – 2010-08-06 05:01:12