SignalR异步操作错误 [英] SignalR An asynchronous operation error

查看:144
本文介绍了SignalR异步操作错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SignalR 1.1.2和我有异步枢纽方法问题。一切正常我与ForeverFrame运输的电脑,但部署在服务器上,并切换到网络插座后的运输,我收到以下错误:


  

这是异步操作不能在这个时间开始。异步操作只能在异步处理程序或模块内或在在页面生命周期的某些事件开始。如果在执行一个页面出现了这种异常,保证页面被标记为LT;%@页面异步=真正的%>


我的枢纽方法code:

 公共异步任务<串GT;的getURL()
    {
        VAR URL =等待MyWebservice.GetMyRoomUrlAsync(Context.User.Identity.Name);        返回URL;
    }

是否支持SignalR与网络插口运输异步方法?

更新:
GetMyRoomUrlAsync code:

 公共静态任务<串GT; GetMyRoomUrlAsync(字符串email)
    {
        VAR TCS =新TaskCompletionSource<串GT;();        VAR的客户=新Onif40.VisualStudioGeneratedSoapClient();        client.GetRoomUrlCompleted + =(S,E)=>
        {
            如果(e.Error!= NULL)
                tcs.TrySetException(e.Error);
            否则,如果(e.Cancelled)
                tcs.TrySetCanceled();
            其他
                tcs.TrySetResult(e.Result);
        };        client.GetRoomUrlAsync(电子邮件);        返回tcs.Task;
    }

在斯蒂芬·克利里澄清我在哪里的问题是,通过重写EAP来解决APM它是微不足道的。

 公共静态任务<串GT; GetMyRoomUrlAsync(字符串email)
    {
        VAR TCS =新TaskCompletionSource<串GT;();        VAR的客户=新Onif40.VisualStudioGeneratedSoapClient();        client.BeginGetRoomUrl(电子邮件,IAR =>
        {
            尝试
            {
                tcs.TrySetResult(client.EndGetRoomUrl(IAR));
            }
            赶上(例外五)
            {
                tcs.TrySetException(E);
            }
        }, 空值);        返回tcs.Task;
    }


解决方案

异步方法支持。但是,你不能使用异步无效异步包装器的 EAP方法的。

这方面的一个常见原因是使用 Web客户端,而不是较新的的HttpClient 。如果这里不是这种情况,就需要发布实施 GetMyRoomUrlAsync

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

解决方案

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.

这篇关于SignalR异步操作错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆