如何在SignalR for ASP.NET Core 2.0中检查HubConnection.StartAsync是否成功? [英] How to check success of HubConnection.StartAsync in SignalR for ASP.NET Core 2.0?

查看:579
本文介绍了如何在SignalR for ASP.NET Core 2.0中检查HubConnection.StartAsync是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SignalR for ASP.NET Core 2.0,客户端(1.0.0-alpha2-final),C#VS2017.

Using SignalR for ASP.NET Core 2.0, Client side (1.0.0-alpha2-final), C# VS2017.

要连接的代码是: await hubConnection.StartAsync(); 由于这是voidawait,我们如何测试客户端是否成功连接?在我的测试中,我在public override async Task OnConnectedAsync()的服务器端有一个断点,但是该断点并非总是 命中,因此我知道客户端的 有时 无法连接.还是完成连接但没有触发OnConnectedAsync()?

To connect the code is: await hubConnection.StartAsync(); since this is an await for a void how can we test if the client did manage to connect ? In my tests I have a breakpoint on the server side in public override async Task OnConnectedAsync() but that breakpoint is not always hit therefore I know that sometimes the client does not connect. Or perhaps it did connect but OnConnectedAsync() was not triggered?

客户端如何知道它已成功建立连接?

How can the client knows it succeeded in establishing the connection?

谢谢

更多信息.在使用localhost时(在VS中进行调试),它会执行触发OnConnectedAsync();通过ngrok连接时触发OnConnectedAsync().也许这与代理/CORS有关,我不熟悉.

More info. It does fire OnConnectedAsync() when using localhost (debugging in VS); it does not fire OnConnectedAsync() when connecting through ngrok. Maybe this is something to do with proxy/CORS, I am not familiar with.

似乎客户端确实确实可以连接,因为如果我将URL更改为不存在的URL,它会给出404.所以看来问题是OnConnectedAsync()不会触发当客户端通过ngrok连接但直接连接到localhost时会触发.

It appears that the Client does indeed connect because if I change the URL to a non existing one it gives a 404. So it appears that the problem is that OnConnectedAsync() does not fire when the client connects through ngrok but does fire when connected directly to localhost.

推荐答案

StartAsync不是void方法,但返回Task.如果客户端未连接StartAsync抛出(返回错误的任务),否则在客户端成功连接后,异步调用将完成而不会出现错误(返回已完成的任务).

StartAsync is not a void method but returns a Task. If the client does not connect StartAsync throws (returns a faulted task) otherwise the async call will complete without an error (returns a completed task) after the client is successfully connected.

这篇关于如何在SignalR for ASP.NET Core 2.0中检查HubConnection.StartAsync是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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