user2449952
user2449952

Reputation: 661

NetMq ( ZeroMq ) code not working as Windows Service

Service are installing properly.

But as soon as I call this LogsConsumer() code, it creates problem.

It gets installed, but doesnt start. It keeps showing "Starting" status

    protected override void OnStart(string[] args)
    {
        LogsConsumer();
    }

    public static void LogsConsumer()
    {
        using (NetMQContext ctx = NetMQContext.Create())
        {
            using (var consumer = ctx.CreatePullSocket())
            {
                consumer.Bind("tcp://localhost:5005");
                while (true)
                {
                    string msg = consumer.ReceiveString();
                }
            }
        }
    }

Upvotes: 1

Views: 1314

Answers (1)

Guido Simone
Guido Simone

Reputation: 7952

OnStart should not call LogsConsumer directly. It should start a new thread which runs the LogsConsumer loop.

The Windows service control manager will not change the status from "Starting" to "Started" until the OnStart method exits. The way you have it coded, OnStart never exits so behavior you are seeing is exactly what I would expect.

BTW - once you have this problem resolved, you may run into a similar problem when you try to stop the service. You will need to modify the loop to support some way to request that it exits, then override OnStop to signal the thread to exit. The pure ZeroMQ way to handle this is to have the thread poll two sockets - your existing external bound socket plus a second inproc socket (see http://zguide.zeromq.org/page:all#Handling-Multiple-Sockets).

The purpose of the second socket is to allow the OnStop method (which is running in a separate service thread) to send a message to your worker thread to stop it.

Upvotes: 2

Related Questions