2013-07-01 151 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,但是我会在以后重复我的结果。 –