MessageQueue.BeginReceive () null ref error - c # - PullRequest
1 голос
/ 13 апреля 2010

Есть служба Windows, которая слушает MSMQ. В методе OnStart есть это

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());
    }
}

где

private MessageQueue _queue = null;

Это работает на моем компьютере, но при развертывании на сервере Windows 2003 и запуске в качестве учетной записи сетевой службы происходит сбой

Исключение 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)

Решено: Оказалось, что Q, который я настроил, мне пришлось явно добавить к нему учетную запись сетевой службы на вкладке безопасности

1 Ответ

2 голосов
/ 13 апреля 2010

Вы видите это конкретное исключение, потому что вы звоните ex.InnerException.ToString(). Свойство InnerException заполняется не всегда (на самом деле оно часто не , и не должно быть).

Вероятно, ваша корневая проблема связана с тем, что учетная запись сетевой службы не имеет разрешений для доступа к очереди (в данном случае для чтения из нее).

Вот код, который поможет вам получить фактическую ошибку в журнале событий:

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());
}
...