为了能够测试与监视windows服务相关的一些逻辑,我创建了ServiceController包装器,通常如下所示:
public class ServiceControllerWrapper : IServiceController
{
public ServiceControllerWrapper(ServiceController controller)
{
this.controller = controller;
}
public void Stop()
{
if(controller == null)
return;
// actually the following code is running in a new thread
// but nothing more
try
{
controller.Stop();
}
catch(...)
{
...
}
}
... similar methods
private readonly ServiceController controller;
}我让控制器为空,但仍然不可能获得NullReferenceException,因为在停止方法开始时检查为null。
这是间歇性的,我得到的例外是:
System.NullReferenceException对象引用未设置为对象的实例。在System.ServiceProcess.ServiceController.Stop().
错误目前只发生在64位Win2008系统上。
在检查为null之后,是否有任何错误或控制器成为null的原因?
编辑:
查看内部的ServiceController代码有帮助。在对服务执行任何操作之前,我调用controller.Refresh,它运行良好。
发布于 2011-04-14 16:12:08
该异常看起来像是在ServiceController.Stop()中发生的空引用。尝试使用.NET反射器查看该方法中正在发生的事情。
https://stackoverflow.com/questions/5666091
复制相似问题