2011-01-27 52 views
3

我定義在ASP.NET web服務2個電話: 1.BeginLengthyProcedure和EndLengthyProcedure(異步Web方法) 2.LengthProcedureSync使用jQuery和JSON消耗Asp.net服務器端異步Web方法

namespace ServiceDemo 
{ 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[System.Web.Script.Services.ScriptService] 
public class AsyncWebService : System.Web.Services.WebService 
{ 
    public delegate string LengthyProcedureAsyncStub(int milliseconds); 

    public string LengthyProcedure(int milliseconds) 
    { 
     System.Threading.Thread.Sleep(milliseconds); 
     return "SuccessAsync"; 
    } 

    public class MyState 
    { 
     public object previousState; 
     public LengthyProcedureAsyncStub asyncStub; 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public IAsyncResult BeginLengthyProcedure(int milliseconds, AsyncCallback cb, object s) 
    { 
     LengthyProcedureAsyncStub stub = new LengthyProcedureAsyncStub(LengthyProcedure); 
     MyState ms = new MyState(); 
     ms.previousState = s; 
     ms.asyncStub = stub; 
     return stub.BeginInvoke(milliseconds, cb, ms); 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string EndLengthyProcedure(IAsyncResult call) 
    { 
     MyState ms = (MyState)call.AsyncState; 
     string res = ms.asyncStub.EndInvoke(call); 
     return res; 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string LengthyProcedureSync(int milliseconds) 
    { 
     System.Threading.Thread.Sleep(milliseconds); 
     return "SuccessSync"; 
    } 
} 
} 

然後我使用JQuery來使用它們,我不想使用ASP.NET AJAX。 LengthProcedureSync工作正常,但LenghtyProcudure沒有工作。有什麼我錯過了嗎?

<script type="text/javascript" src="jquery-1.4.4.min.js"></script> 

<script type="text/javascript"> 
    function testJson() { 
     $.ajax({ 
      type: "POST", 
      //Didn't work 
      url: "AsyncWebService.asmx/LengthyProcedure", 
      //Work 
      //url: "AsyncWebService.asmx/LengthyProcedureSync", 
      data: "{'milliseconds':'2000'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       $("#jsonResponse").html(msg.d); 
      }, 
      error: function (msg) { 

      } 

     }); 
    }; 

</script> 
+0

你能從jQuery調用BeginLengthyProcedure並從EndLengthyProcedure獲取結果嗎?怎麼樣? – 2011-04-07 01:34:04

+1

你有沒有解決這個問題?我有同樣的問題。 – Overload119 2011-06-09 14:50:35

回答

0

改變這種

public string LengthyProcedure(int milliseconds) 
    { 
     System.Threading.Thread.Sleep(milliseconds); 
     return "SuccessAsync"; 
    } 

這樣:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string LengthyProcedure(int milliseconds) 
    { 
      System.Threading.Thread.Sleep(milliseconds);   
      return "SuccessAsync"; 
    } 
+1

然後我認爲客戶端會直接調用這個同步Web方法,而不是調用異步Web方法。 – 2011-01-27 14:51:51

1

不幸的是,在.NET Framework的ASP.NET AJAX Web服務處理器(其用於JSON)不提供支持用於異步Web服務調用,而默認的ASP.NET Web服務處理程序執行此操作。有一本由Omar AL Zabir編寫的「使用ASP.NET 3.5構建Web 2.0門戶」的優秀書籍,描述了ASP.NET AJAX Web服務處理程序的這一缺陷的解決方法(請參閱第177頁的第7章)。 MSDN上還有一個示例章節。希望這可以幫助!