2008-12-15 95 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