2013-07-01 198 views
2

我正在使用SignalR 1.1.2,並且遇到了異步集線器方法的問題。一切正常,我與ForeverFrame運輸PC上,但部署在服務器上,並切換到網絡套接字運輸後,我收到以下錯誤:SignalR異步操作錯誤

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

我的樞紐方法代碼:

public async Task<string> getUrl() 
    { 
     var url = await MyWebservice.GetMyRoomUrlAsync(Context.User.Identity.Name); 

     return url; 
    } 

是否支持在SignalR異步方法與網絡插座運輸?

更新: GetMyRoomUrlAsync代碼:

public static Task<string> GetMyRoomUrlAsync(string email) 
    { 
     var tcs = new TaskCompletionSource<string>(); 

     var client = new Onif40.VisualStudioGeneratedSoapClient(); 

     client.GetRoomUrlCompleted += (s, e) => 
     { 
      if (e.Error != null) 
       tcs.TrySetException(e.Error); 
      else if (e.Cancelled) 
       tcs.TrySetCanceled(); 
      else 
       tcs.TrySetResult(e.Result); 
     }; 

     client.GetRoomUrlAsync(email); 

     return tcs.Task; 
    } 

後斯蒂芬·克利裏澄清我在哪裏的問題是,通過重寫EAP到APM解決它是微不足道的。

public static Task<string> GetMyRoomUrlAsync(string email) 
    { 
     var tcs = new TaskCompletionSource<string>(); 

     var client = new Onif40.VisualStudioGeneratedSoapClient(); 

     client.BeginGetRoomUrl(email, iar => 
     { 
      try 
      { 
       tcs.TrySetResult(client.EndGetRoomUrl(iar)); 
      } 
      catch (Exception e) 
      { 
       tcs.TrySetException(e); 
      } 
     }, null); 

     return tcs.Task; 
    } 
+0

不好意思問你一件事。我知道EAP意味着基於事件的異步模式,但APM意味着異步過程管理? – Mou

+0

不,APM意味着異步編程模型(https://msdn.microsoft.com/en-us/library/ms228963.aspx) –

+0

感謝Martin。你的GetMyRoomUrlAsync()函數代碼有點難以理解。可能所有的代碼都不存在。什麼時候GetRoomUrlCompleted()委託會被調用?後來你改變方法,因爲在稍後的示例中你使用BeginGetRoomUrl()。 – Mou

回答

5

async支持的方法。但是,您不能在EAP methods周圍使用async voidasync包裝紙。

這種情況的一個常見原因是使用WebClient代替較新的HttpClient。如果情況並非如此,則需要發佈GetMyRoomUrlAsync的實施。

+0

我在GetMyRoomUrlAsync中使用EAP方法,但是讓我感到困惑的是,它在不使用websocket傳輸時工作正常。 –

+0

在你的web.config中,''httpRuntime'上的'targetFramework'設置爲4.5嗎? –

+0

是的,我喜歡。我從標準的ASP.NET頁面(with async = true)調用這個方法沒有任何問題。我仍然懷疑運輸,所以我會嘗試調用除了指定的websockets以外的其他運輸方式的異步方法。不幸的是,我不能那樣做atm,但是我會在以後重複我的結果。 –