【问题标题】:C# service: setup returns error 1001. Source MyService already exists on the local computer. EventID 11001C# 服务:安装程序返回错误 1001。源 MyService 已存在于本地计算机上。事件 ID 11001
【发布时间】:2013-10-28 15:03:47
【问题描述】:

目前我尝试创建一个 C# (2010) 服务。

我创建了一个 Visual Studio 设置项目。每当我尝试安装该服务时,我都会收到消息

Error 1001.  Source MyService already exists on the local computer.

在事件日志中我找到以下信息:

- System 

  - Provider 

   [ Name]  MsiInstaller 

  - EventID 11001 

   [ Qualifiers]  0 

   Level 2 

   Task 0 

   Keywords 0x80000000000000 

  - TimeCreated 

   [ SystemTime]  2013-10-28T14:28:23.000000000Z 

   EventRecordID 206256 

   Channel Application 

   Computer <MyComputer> 

  - Security 

   [ UserID]  S-1-5-21-703477020-2137377117-2121179097-8027 


- EventData 

   Product: MyServiceSetup -- Error 1001. Error 1001. Source MyService already exists on the local computer. 
   (NULL) 
   (NULL) 
   (NULL) 
   (NULL) 
   (NULL) 

   7B30353636304544462D374645372D344243312D414442422D4534424244343645393646457D 

我尝试了以下命令(在具有管理员权限的命令窗口中):

InstallUtil /u MyService.exe

sc delete MyService

但我不断收到此错误。我以足够的权限安装。而且我完全不知道我必须在哪里寻找解决方案。

有人可以帮帮我吗?我几乎没有头发可以拔了......

谢谢!!!

【问题讨论】:

  • stackoverflow.com/questions/9895586/… 这可以帮助您解决问题
  • 尝试了所有...但是在我的 services.msc 中找不到该服务。不在添加/删除程序中,也不在注册表中。但是,我有一个该名称的事件日志源(由安装程序创建)。在添加它之前,我检查它是否存在。我已将该代码放在注释中,进行了新构建,但得到了同样的错误。

标签: c# runtime-error setup-project


【解决方案1】:

我自己遇到了这个问题,但找不到解决方案,所以经过几天的研究和测试并尝试了不同的方法,我终于找到了解决这个问题的解决方案!

当您使用 Windows InstallUtil.exe 安装服务时,它会创建一个与您的服务名称相同的事件日志源并将其添加到“应用程序”事件日志中。但是,当它卸载服务时,它不会删除此源。因此,当您尝试重新安装它时,它会尝试再次添加源,但由于它已经存在,因此会出错,并且由于出现错误,它会回滚安装。

我所做的是在我的服务中添加了用于自我安装/卸载的代码,以及一种清理它使用的事件日志的方法,以及 Windows 在第一次安装它时创建的事件日志。这种额外的方法是为我解决的问题。使用以下代码,几乎只需运行 -delevent 几次,直到它显示不存在任何内容。然后尝试再次安装,它应该可以工作。

在具有管理员权限的命令提示符中调用这些:

Service1.exe -install
Service1.exe -uninstall
Service1.exe -delevent

这是我的代码:

程序.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;

namespace WindowsService1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                foreach (string item in args)
                {
                    switch (item.ToLower())
                    {
                        case "-install":
                            Install(false, args); break;
                        case "-uninstall":
                            Install(true, args); break;
                        case "-delevent":
                            DeleteEventStuff(); break;
                        default:
                            Console.Error.WriteLine("Argument not expected: " + item); break;
                    }
                }
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                ServiceBase.Run(ServicesToRun);
            }

        }

        // This is the method that does the actual installing/uninstalling of the service for Windows
        static void Install(bool uninstall, string[] args)
        {
            try
            {
                Console.WriteLine(uninstall ? "Uninstalling Service" : "Installing Service");
                using (AssemblyInstaller inst = new AssemblyInstaller(typeof(MyProjectInstaller).Assembly, args))
                {
                    IDictionary state = new Hashtable();
                    inst.UseNewContext = true;
                    try
                    {
                        if (uninstall)
                        {
                            inst.Uninstall(state);
                            Console.WriteLine();
                            Console.WriteLine("Uninstall Successful");
                        }
                        else
                        {
                            inst.Install(state);
                            Console.WriteLine();
                            Console.WriteLine("Installed Successfuly. Now Commiting...");
                            inst.Commit(state);
                            Console.WriteLine();
                            Console.WriteLine("Commit Successful");
                        }
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            Console.WriteLine();
                            Console.WriteLine("ERROR: " + ex.Message);
                            Console.WriteLine();
                            Console.WriteLine("Rolling back service installation...");
                            inst.Rollback(state);
                            Console.WriteLine();
                            Console.WriteLine("Rollback Successful");
                        }
                        catch { }
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }

        // This is the method that cleans up the event logging that Windows creates on install
        static void DeleteEventStuff()
        {
            // Delete the event log stuff that the service actually uses.
            if (System.Diagnostics.EventLog.SourceExists(Service1.eventSource))
            {
                try
                {
                    System.Diagnostics.EventLog.DeleteEventSource(Service1.eventSource);
                    Console.WriteLine();
                    Console.WriteLine("Event source deleted successfully!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("Error deleting event source: " + ex.Message);
                }
            }
            else
                Console.WriteLine(); Console.WriteLine("The event source '" + Service1.eventSource + "' does not exist.");

            if (System.Diagnostics.EventLog.Exists(Service1.eventLog))
            {
                try
                {
                    System.Diagnostics.EventLog.Delete(Service1.eventLog);
                    Console.WriteLine();
                    Console.WriteLine("Event log deleted successfully!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("Error deleting event log: " + ex.Message);
                }
            }
            else
                Console.WriteLine(); Console.WriteLine("The event log '" + Service1.eventLog + "' does not exist.");

            // Delete the event log stuff that windows installer utilities thinks the service will use.
            if (System.Diagnostics.EventLog.SourceExists(Service1._serviceName))
            {
                try
                {
                    System.Diagnostics.EventLog.DeleteEventSource(Service1._serviceName);
                    Console.WriteLine();
                    Console.WriteLine("Event source deleted successfully!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("Error deleting event source: " + ex.Message);
                }
            }
            else
                Console.WriteLine(); Console.WriteLine("The event source '" + Service1._serviceName + "' does not exist.");

            if (System.Diagnostics.EventLog.Exists(Service1._serviceName))
            {
                try
                {
                    System.Diagnostics.EventLog.Delete(Service1._serviceName);
                    Console.WriteLine();
                    Console.WriteLine("Event log deleted successfully!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("Error deleting event log: " + ex.Message);
                }
            }
            else
                Console.WriteLine(); Console.WriteLine("The event log '" + Service1._serviceName + "' does not exist.");

            // Delete the actual custom event log file stored on the hard drive if it exists so it can be recreated on re-install
            if (System.IO.File.Exists(@"%SystemRoot%\System32\winevt\Logs\" + Service1.eventLog + ".evtx"))
            {
                try
                {
                    System.IO.File.Delete(@"%SystemRoot%\System32\winevt\Logs\" + Service1.eventLog + ".evtx");
                    Console.WriteLine();
                    Console.WriteLine("Event log found deleted from hard drive successfully!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("Error deleting event log from hard drive: " + ex.Message);
                }
            }
            else
                Console.WriteLine(); Console.WriteLine("The event log '" + Service1._serviceName + "' file was not found on the hard drive.");
        }
    }
}

【讨论】:

  • 我有相同的代码来安装我的服务,但它在 inst.Install(state); 上失败了告诉我它已经存在,但它不可能存在。这是我第一次尝试安装它。有什么想法吗?
  • 救了我!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多