Skip to content

Console application using async/await stops receiving after sending #3895

@ThatRendle

Description

@ThatRendle

I was working on a spike using a pair of console applications to send and receive messages, and hit a bit of odd behavior. In the subscriber application, once it had invoked a method on the Hub using an await, it stopped receiving new messages. Initially this meant it never received messages because it called a Subscribe method right away, but digging into it I found that it would receive quite happily until it called a method.

When the method is invoked with a .Wait() instead of await everything is fine.

I'm not sure if this is to do with the way threads are handled in console applications (as opposed to WPF applications which have a SynchronizationContext), or is down to a problem in the C# SignalR client.

Expected behavior

Would expect to be able to await hubProxy.Invoke("Foo"); and continue to receive messages.

Actual behavior

Using await hubProxy.Invoke("Foo"); breaks the connection somehow.

Steps to reproduce

class Program
{
  static void Main()
  {
    Run().Wait();
  }

  static async Task Run()
  {
    Console.WriteLine("[Subscriber] press Enter to enter...");
    Console.ReadLine();

    var hubConnection = new HubConnection("http://localhost:39103/");
    var hubProxy = hubConnection.CreateHubProxy("MyHub");
    hubProxy.On<string>("OnMessage", msg =>
    {
      Console.WriteLine($"Received: {msg}");
    });

    await hubConnection.Start();

    // Receiving messages quite happily at this point

    Console.WriteLine("And press Enter again to break the application...");
    Console.ReadLine();

    await hubProxy.Invoke("Foo", "Bar");

    // NO MESSAGES FOR YOU!

    Console.WriteLine("And press Enter to Exit, because computers...");
    Console.ReadLine();
  }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions