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# - MySQL .NET Connector 连接失败

c# - 如何在远程服务器上运行命令行程序

c# - Windows 服务能否正常关闭需要数小时?

c# - 复合主键部分的 EF 一对多

c# - 如何在 C# 中实现绘图器

c# - 如何使用 C# 从 Windows 服务运行 EXE 程序?

c# - 并行运行多个 EntityFramework 数据库查询

c# - 如何将松散耦合和可扩展的设计与可能的异步实现相结合?

asynchronous - TaskCompletionSource - 尝试理解无线程异步工作

c# - 如何反序列化嵌套在另一个标签的文本部分中的标签?