SignalR An asynchronous operation error

亡梦爱人 提交于 2019-12-14 03:57:56

问题


I'm using SignalR 1.1.2 and I have problem with async hub method. Everything works fine on my PC with ForeverFrame transport but after deploying on server and switching to web sockets transport I receive following error:

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" %>.

My hub method code:

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

        return url;
    }

Are async methods supported in SignalR with web-sockets transport?

Update: GetMyRoomUrlAsync code:

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;
    }

After Stephen Cleary clarified me where the problem was, solving it by rewriting EAP to APM was trivial.

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;
    }

回答1:


async methods are supported. However, you cannot use async void or async wrappers around EAP methods.

One common cause of this is using WebClient instead of the newer HttpClient. If that's not the case here, you would need to post the implementation of GetMyRoomUrlAsync.



来源:https://stackoverflow.com/questions/17403973/signalr-an-asynchronous-operation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!