Search code examples
.net-corewampsharp

can't use await on caller and callee?


I'm writing a simplest caller console app according to this article with core3.1. When i changed channel.Open().Wait(5000); to await channel.Open();, it's stucked at line proxy.Ping();.

And this is also found on a callee:

Task openTask = channel.Open();
openTask.Wait();

can't replace with await channel.Open();

Task<IAsyncDisposable> registrationTask = realm.Services.RegisterCallee(instance);
registrationTask.Wait();

can't replace with await realm.Services.RegisterCallee(instance);

Once await is used, the program always gets stuck at line proxy.Ping();

The solution zipfile.

Why?Isn't await waiting?


Solution

  • Your method call proxy.Ping() blocks the Websocket thread. If you use async syntax, you should be careful not to block the Websocket thread. Use the await proxy.PingAsync() version. If you need to make blocking calls like Console.ReadLine(), make sure to release the Websocket thread by calling await Task.Yield() before making your blocking call.