NullReferenceException at ServiceController.Stop
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 o开发者_StackOverflow中文版n 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.
That exception looks like the null reference occurs inside ServiceController.Stop()
. Try using .NET Reflector to look at what is happening inside that method.
精彩评论