Async NetMQ throws "NetMQRuntime must be created before calling async functions"

82 views Asked by At

I have two .Net 7 console apps using NetMQ (v4.0.1.13) and both have similar program classes - only the async task differs:

class Program
{

    private static readonly CancellationTokenSource cts = new();

    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
        var runtime = new NetMQRuntime();
        runtime.Run(cts.Token, <async client | server task here>);
    }

    private static void CurrentDomain_ProcessExit(object? sender, EventArgs e)
    {
        try
        {
            cts.Cancel();
        }
        catch { }
        finally
        {
            NetMQConfig.Cleanup();
        }
    }
}

The "client" has the following:

using var requestSocket = new RequestSocket();
requestSocket.Connect("tcp://localhost:5555");

...

requestSocket.SendMultipartMessage(newBlockMessage); // Step 1
// This line throws the "NetMQRuntime must be created before calling async functions" exception!
var result = await requestSocket.ReceiveFrameStringAsync(); // Step 4

The "server" has the following lines:

using var responseSocket = new ResponseSocket();
responseSocket.Bind("tcp://localhost:5555");

...
NetMQMessage messages = await responseSocket.ReceiveMultipartMessageAsync(); // Step 2

...

//This line results in the client exception above
ResponseSocket.SendFrame(SystemEvent.EventTypes.NewBlockAndTransferEventStored.ToString()); // Step 3

I've been struggling with this for a while and cannot understand what is different in the initialization code in the program classes for the client and server. I've outlined the message flow as "Step (n)" in order to clarify the message exchanges. It feels like I'm missing something very basic in the client initialization but I fail to identify it. For a reason unknown, the "server" app seems to handle aync(s) beautifully (it doesn't complain) while the "client" throws an exception in step 4 receiving the "server" reply.

0

There are 0 answers