我正在使用 SignalR 1.1.2,但异步集线器方法有问题。使用 ForeverFrame 传输在我的 PC 上一切正常,但在服务器上部署并切换到 Web 套接字传输后,我收到以下错误:
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 中是否支持具有 Web 套接字传输的异步方法?
更新: 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;
}
在 Stephen Cleary 向我澄清了问题所在之后,通过将 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;
}
最佳答案
支持
异步
方法。但是,您不能在 EAP methods 周围使用 async void
或 async
包装器。 .
造成这种情况的一个常见原因是使用 WebClient
而不是较新的 HttpClient
。如果此处情况并非如此,您需要发布 GetMyRoomUrlAsync
的实现。
关于asp.net - SignalR 异步操作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17403973/