简体   繁体   中英

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 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.

That exception looks like the null reference occurs inside ServiceController.Stop() . Try using .NET Reflector to look at what is happening inside that method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM