MessageQueue.BeginReceive()空引用错误-c#

MessageQueue.BeginReceive()空引用错误-c#,c#,msmq,C#,Msmq,具有侦听msmq的windows服务。在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.Receiv

具有侦听msmq的windows服务。在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服务器并作为网络服务帐户运行时,它会失败

例外记录:

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必须在安全选项卡下显式地将网络服务帐户添加到其中

您看到了特定的异常,因为您正在调用
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());
}

请发布
异常的内容,包括堆栈跟踪。