我正在使用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;
}
不好意思問你一件事。我知道EAP意味着基於事件的異步模式,但APM意味着異步過程管理? – Mou
不,APM意味着異步編程模型(https://msdn.microsoft.com/en-us/library/ms228963.aspx) –
感謝Martin。你的GetMyRoomUrlAsync()函數代碼有點難以理解。可能所有的代碼都不存在。什麼時候GetRoomUrlCompleted()委託會被調用?後來你改變方法,因爲在稍後的示例中你使用BeginGetRoomUrl()。 – Mou