Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在尝试卸载服务c时,对象引用未设置为对象的实例#_C#_Object_Reference - Fatal编程技术网

C# 在尝试卸载服务c时,对象引用未设置为对象的实例#

C# 在尝试卸载服务c时,对象引用未设置为对象的实例#,c#,object,reference,C#,Object,Reference,我有以下停止和取消安装服务的代码。 它会正确停止服务,但在我尝试取消安装时会出现以下错误: System.NullReferenceException: Object reference not set to an instance of an object. at System.Configuration.Install.Installer.Uninstall(IDictionary savedState) at System.ServiceProcess.ServiceInsta

我有以下停止和取消安装服务的代码。 它会正确停止服务,但在我尝试取消安装时会出现以下错误:

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Configuration.Install.Installer.Uninstall(IDictionary savedState)
   at System.ServiceProcess.ServiceInstaller.Uninstall(IDictionary savedState)
   at UpdateOrca.FuncoesUpdater.StopService(String serviceName, Int32 timeoutMilliseconds, Boolean Unninstall) in C:\Users\me\Downloads\UpdateOrcaV2013\UpdateOrca\UpdateOrca\FuncoesUpdater.cs:line 165
代码:

该方法实际上需要一些有关您正在卸载的内容的上下文信息。这通常可以作为
IDictionary
对象传递给
Uninstall()
方法,但如果您决定改为传递
null
,则可能需要显式设置上下文:

// Build your uninstaller
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
if (Unninstall)
{
      // Set a context (using a specific file to log uninstall info)
      ServiceInstallerObj.Context = new InstallContext("{path-to-log-file}", null);
      // Continue setting your service and uninstalling it
      ServiceInstallerObj.ServiceName = serviceName;
      ServiceInstallerObj.Uninstall(null); 
}

如果这不起作用,你可以考虑尝试类似于上面提到的方法。

// Build your uninstaller
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
if (Unninstall)
{
      // Set a context (using a specific file to log uninstall info)
      ServiceInstallerObj.Context = new InstallContext("{path-to-log-file}", null);
      // Continue setting your service and uninstalling it
      ServiceInstallerObj.ServiceName = serviceName;
      ServiceInstallerObj.Uninstall(null); 
}