我正在启动HubConnection。首先我得到一个新的HubConnection实例, 然后我创建了一个名为=“fileHub”的新IHubProxy,迄今为止这么好。任务等待失败
我的问题是位于(或在)等待函数ContinueWith,我尝试启动连接,并写入控制台wheater启动成功与否。 connection.Start()成功并且“连接”被写入控制台。 在Console.WriteLine(“已连接”)执行完成后添加的代码也没有问题。
但是任务永远不会结束,所以调用HandleSignalRAsync()方法的Client类等待完成失败。
添加返回;或task.dispose();没有解决我的问题。
public static async Task HandleSignalRAsync()
{
connection = new HubConnection("http://localhost:12754");
myHub = connection.CreateHubProxy("fileHub");
await connection.Start().ContinueWith(
task =>
{
if (task.IsFaulted)
{
var ex = task.Exception.GetBaseException();
Console.WriteLine("There was an error opening the connection: {0}", ex);
}
else
{
Console.WriteLine("Connected");
}
});
}
我把我的解决方案的另一类与TaskAwaiter方法:
Functions.HandleSignalRAsync().GetAwaiter().GetResult();
与调用它:
Functions.HandleSignalRAsync().Wait();
不起作用了。
为什么当你已经使用'await'时你使用'ContinueWith'? 'await' *之后的代码是*延续。如果您想捕获异常,请将该调用包装在异常处理程序中 –