【问题标题】:NullReferenceException at ServiceController.StopServiceController.Stop 处的 NullReferenceException
【发布时间】:2011-04-14 16:02:02
【问题描述】:

为了能够测试一些与监视 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,因为在 Stop 方法开始时检查为空。

它间歇性地发生,我得到的例外是:

System.NullReferenceException 你调用的对象是空的。 在 System.ServiceProcess.ServiceController.Stop()。

目前仅在 64 位 Win2008 系统上出现错误

在检查不为空后,我是否有任何错误或控制器变为空的任何原因?

编辑:

查看 ServiceController 代码会有所帮助。在对服务进行任何操作之前,我调用了 controller.Refresh,它运行良好。

【问题讨论】:

  • 如果堆栈跟踪显示 ServiceController.Stop 作为堆栈跟踪的一部分,则表明异常是在该方法内部而不是由您的代码引发的。
  • @Jon Skeet 在 MSDN 中,我只看到 ServiceController.Stop 可能引发的两种类型的异常 - Win32Exception 和 InvalidOperationException。 NullReferenceException 可能与其中任何一个有关吗?
  • 不应该被抛出,但它可能表明框架中存在错误。

标签: c# winforms multithreading windows-services wrapper


【解决方案1】:

该异常看起来像空引用发生在内部 ServiceController.Stop()。尝试使用 .NET Reflector 查看该方法内部发生的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多