0
爲了能夠測試與監控Windows服務相關的一些邏輯,我創建ServiceController的包裝,一般看起來像:的NullReferenceException在ServiceController.Stop
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;
}
我讓控制器爲空,但它仍然是由於在Stop方法開始時檢查爲null,因此不可能得到NullReferenceException。
它發生間歇性地和我得到的例外是:
System.NullReferenceException 對象引用不設置到對象的實例。 at System.ServiceProcess.ServiceController.Stop()。
錯誤目前只能在64位的Win2008系統
發生有我做的任何錯誤或任何理由控制器檢查是不是空後成爲空?
編輯:
展望ServiceController的代碼中的幫助。在對服務進行任何操作之前,我打電話給controller.Refresh,它運行良好。
如果堆棧跟蹤將ServiceController.Stop顯示爲堆棧跟蹤的一部分,則表明該異常正在*該方法內部引發,而不是由您的代碼引發。 – 2011-04-14 16:07:45
@Jon Skeet在MSDN中,我只看到兩種類型的可能由ServiceController.Stop引發的異常 - Win32Exception和InvalidOperationException。可能NullReferenceException與其中的任何一個有關? – username 2011-04-14 16:11:32
它*不應該被拋出,但它可能表示框架內的錯誤。 – 2011-04-14 16:16:31