c# - 为什么我无法安装我的 Windows 服务 - 指定的服务已经存在

标签 c# windows-services task-parallel-library

我希望此时我能在我的电脑上放一枚手榴弹。我很沮丧,因为我不明白为什么我的应用程序无法使用 installUtil 安装。

我刚刚浏览了这个链接:Windows Service Install Ends in Rollback不幸的是,那里的善意建议对我的情况没有帮助,在考虑了 SO 的好人在该链接和其他人上发布的所有答案后,产生了以下错误。

我在网络上寻找过任务并行处理模式的最佳实践,但目前没有任何帮助。我在尝试安装时遇到的最新错误如下:

.exe assembly's progress. The file is located at E:\xxx\MyService\Service_V2.InstallLog. Installing assembly 'E:\xxx\MyService\Service_V2.exe'. Affected parameters are:
logtoconsole = logfile = E:\xxx\MyService\Service_V2.InstallLog
assemblypath = E:\xxx\MyService\Service_V2.exe Installing service Service V2... Service Service V2 has been successfully installed. Creating EventLog source Service V2 in log Application... Installing service Service V2... Creating EventLog source Service V2 in log Application...

An exception occurred during the Install phase. System.ComponentModel.Win32Exception: The specified service already exists

The Rollback phase of the installation is beginning. See the contents of the log file for the E:\xxx\MyService\Service_V2 .exe assembly's progress. The file is located at E:\xxx\MyService\Service_V2.InstallLog. Rolling back assembly 'E:\xxx\MyService\Service_V2.exe'. Affected parameters are:
logtoconsole = logfile = E:\xxx\MyService\Service_V2.InstallLog
assemblypath = E:\xxx\MyService\Service_V2.exe Restoring event log to previous state for source Service V2. Restoring event log to previous state for source Service V2. Service Service V2 is being removed from the system... Service Service V2 was successfully removed from the system.

The Rollback phase completed successfully.

The transacted install has completed. The installation failed, and the rollback has been performed.

也没有任何内容写入事件日志。

这是我的 OnStart() 方法:

protected override void OnStart(string[] args)
        {
            var tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;

            ErrorLogFileName = "Service_V2Errors" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            Service_V2LogFile = "Service_V2Log" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";

            ErrorLogPath = ConfigurationManager.AppSettings["Errorpath"].ToString();
            CheckBatchRecord = Convert.ToInt32(ConfigurationManager.AppSettings["CheckBatchTime"].ToString());

            if (!Directory.Exists(ErrorLogPath))
            {
                Directory.CreateDirectory(ErrorLogPath);
            }

            LogMessage("Starting Service " + DateTime.Now.ToString());

            _ErrorLog = new StreamWriter(ErrorLogPath + "//" + ErrorLogFileName, true);
            _ErrorLog.WriteLine("Error, Location, AdditionalInformation", true);
            _ErrorLog.Close();

            var t = Task.Run(() => Service_V2Start(), token);

            try
            {
                t.Wait();
            }
            catch (AggregateException e)
            {
                LogMessage("Exception messages:");
                foreach (var ie in e.InnerExceptions)
                    LogMessage(ie.GetType().Name + " : " + ie.Message);

                LogMessage("\nTask status: " + t.Status);       
            }
            finally
            {
                tokenSource.Dispose();
            }          
        }

我还为编译的最终安装文件设置了编译模式。

我已经执行了“sc delete Servie V2”,我还检查了服务控制台,但那里没有列出此类服务。

我也试过 InstallUtil.exe -u 命令来卸载,但我仍然得到这个傻瓜错误。我现在该怎么办?

最佳答案

  1. 确保您的 Program.cs 文件看起来像这样:

    static class Program
    {
        static void Main()
        {
            var service = new YourServiceName();
            ServiceBase.Run(service);
        }
    }
    
  2. InitializeComponent() 方法中,确保 ServiceName 属性值与 中的 ServiceName 相同>ProjectInstaller.cs

     this.ServiceName = "MyServiceName";//in the YourServiceName partial class
    
     this.myServiceInstaller.ServiceName = "MyServiceName";//in the installer
    
  3. 确保您只有一个安装程序。

  4. 在您创建的用于安装和卸载服务的批处理文件中,确保您指向正确的 InstallUtil.exe

    对于 64 位架构,您可以使用 - C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe

    对于 32 位架构,您可以使用 - C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

  5. 示例 InstallService.bat 文件:

    "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe""PathToTheExecutables\MyServiceName.exe" 暂停

  6. 示例 UninstallService.bat 文件:

    "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe"/u "PathToTheExecutables\MyServiceName.exe" 暂停

关于c# - 为什么我无法安装我的 Windows 服务 - 指定的服务已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220645/

相关文章:

c# - 如何从 Windows 服务 C# 取消关机

configuration - 为 Windows 服务保留用户可编辑设置的最佳方式是什么?

windows - 无法将 Go 应用程序 exe 作为 Windows 服务启动

C# 开发人员开始 SICP - 建议的开发环境?

c# - 当某些任务可以为空时如何使用 WhenAll?

c# - 为什么任务甚至在等待中完成

c# - 如何在后台运行非必要的计算?

c# - PInvoke 问题

c# - Command.ExecuteNonQuery()始终返回-1

c# - 在 .NET Framework 和 .NET Core 之间自动传递和拉取 JSON 数据