SignalR An asynchronous operation error

2019-07-16 00:52发布

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条回答
Root(大扎)
2楼-- · 2019-07-16 01:36

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.

查看更多
登录 后发表回答