tags:

views:

137

answers:

1

Have a windows service that listens to a msmq. In the OnStart method is have this

protected override void OnStart(string[] args)
{
    try
    {
        _queue = new MessageQueue(_qPath);//this part works as i had logging before and afer this call

        //Add MSMQ Event
        _queue.ReceiveCompleted += new ReceiveCompletedEventHandler(queue_ReceiveCompleted);//this part works as i had logging before and afer this call

        _queue.BeginReceive();//This is where it is failing - get a null reference exception
    }
    catch(Exception ex)
    {
        EventLogger.LogEvent(EventSource, EventLogType, "OnStart" + _lineFeed +
             ex.InnerException.ToString() + _lineFeed + ex.Message.ToString());
    }
}

where

private MessageQueue _queue = null;

This works on my machine but when deployed to a windows 2003 server and running as Network service account, it fails

Exception recvd:

Service cannot be started. System.NullReferenceException: Object reference not set to an instance of an object.
at MYService.Service.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

Solved: Turned out that the Q that i set up, I had to explicitly add Network Service account to it under security tab

+1  A: 

You're seeing that particular exception because you're calling ex.InnerException.ToString(). The InnerException property is not always populated (in fact, it frequently isn't, nor should it be).

Your root problem is likely that the Network Service account doesn't have permissions to access the queue (in this case, read from it).

Here's some code that will help you get the actual error in your event log:

catch(Exception ex)
{
    Exception e = ex;
    StringBuilder message = new StringBuilder();

    while(e != null)
    {
        if(message.Length > 0) message.AppendLine("\nInnerException:");

        message.AppendLine(e.ToString());

        e = e.InnerException;
    }

    EventLogger.LogEvent(EventSource, EventLogType, "OnStart" + _lineFeed +
         message.ToString());
}
Adam Robinson