ServiceController.Stop 处的 NullReferenceException
为了能够测试与监视 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;
}
我让控制器为 null,但仍然不可能获得 NullReferenceException,因为在 Stop 开始时检查为 null方法。
它间歇性地发生,我得到的异常是:
System.NullReferenceException 你调用的对象是空的。 在 System.ServiceProcess.ServiceController.Stop() 处。
目前仅在 64 位 Win2008 系统上发生错误
我是否有任何错误或控制器在检查不为空后变为空的任何原因?
编辑:
查看 ServiceController 代码内部会有所帮助。在对服务进行任何操作之前,我调用controller.Refresh,它运行良好。
In order to be able to test some logic related with monitoring windows services I created ServiceController wrapper that in general looks like:
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;
}
I let the controller to be null but it is still impossible to get NullReferenceException because of checking to null at the start of the Stop method.
It happens intermittently and the exception I'm getting is:
System.NullReferenceException
Object reference not set to an instance of an object.
at System.ServiceProcess.ServiceController.Stop().
Error currently occur only on 64 bit Win2008 system
Is there any mistakes I'm doing or any reason for controller to become null after checking to be not null?
EDIT:
Looking inside the ServiceController code helped. Before doing any operation with services I'm calling controller.Refresh and it works well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该异常看起来像是空引用发生在
ServiceController.Stop()
内部。尝试使用 .NET Reflector 来查看该方法内部发生了什么。That exception looks like the null reference occurs inside
ServiceController.Stop()
. Try using .NET Reflector to look at what is happening inside that method.