拥有一个监听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,我必须在安全选项卡
下明确添加网络服务帐户答案 0 :(得分:2)
您正在看到特定的例外,因为您正在调用ex.InnerException.ToString()
。 InnerException
属性并不总是填充(事实上,它经常不是,也不应该)。
您的root问题可能是网络服务帐户没有访问队列的权限(在这种情况下,从中读取)。
这里有一些代码可以帮助您在事件日志中获得实际错误:
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());
}