2

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;
    }
Martin Vich
  • 1,062
  • 1
  • 7
  • 22
  • sorry to ask u one things. i know EAP means Event-based Asynchronous Pattern but does APM means Asynchronous Process Management ? – Mou Jan 30 '15 at 11:03
  • No, APM means Asynchronous Programming Model (https://msdn.microsoft.com/en-us/library/ms228963.aspx) – Martin Vich Jan 30 '15 at 11:34
  • thanks Martin. your GetMyRoomUrlAsync() function code is bit hard to understand. probably all code is not there. when GetRoomUrlCompleted() delegate will be called ? later u change approach because in later sample u use BeginGetRoomUrl(). – Mou Jan 30 '15 at 11:53
  • There is no GetRoomUrlComleted() in the second example, since it's showing APM pattern which uses BeginMethod & EndMethod... If you mean where is the code that gets called after the url is retrieved by the webservice then it's the iar => { .... } – Martin Vich Jan 30 '15 at 12:08

1 Answers1

5

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.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I'm using EAP methods in GetMyRoomUrlAsync, but what confused me is that it works fine when not using websocket transport. – Martin Vich Jul 01 '13 at 12:29
  • In your web.config, do you have `targetFramework` set to 4.5 on `httpRuntime`? – Stephen Cleary Jul 01 '13 at 12:47
  • Yes, I do. I call this method from standart ASP.NET Page (with async=true) without any problem. I still have suspicion on transport so I'll try calling async method with other transport than websockets specified. Unfortunately I can't do that atm, but I'll posty my results later. – Martin Vich Jul 01 '13 at 13:03
  • 2
    If the `async` method is executed within the context of an HTTP request, then it won't raise this exception. WebSockets use a different context, which causes this exception. However, I agree that the difference in behavior can be confusing, and I'll raise an issue with the SignalR team. – Stephen Cleary Jul 01 '13 at 13:09