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);
}