C#service:setup返回错误1001.源MyService已存在于本地计算机上。 EventID 11001

时间:2013-10-28 15:03:47

标签: c# runtime-error setup-project

目前我尝试创建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 

我尝试了以下命令(在带有admin-rights的命令窗口中):

InstallUtil /u MyService.exe

sc delete MyService

但我一直收到这个错误。我安装了足够的权利。我完全不知道在哪里寻找解决方案。

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

感谢名单!!!

1 个答案:

答案 0 :(得分:2)

我自己遇到了这个问题而无法找到解决方案,所以经过几天的研究和测试以及尝试不同的事情后,我终于想出了解决这个问题的解决方案!

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

我所做的是我在自助安装/卸载服务中添加了代码,以及清理它使用的事件日志记录的方法,以及Windows在第一次安装时创建的事件。这个额外的方法是为我修复的。使用以下代码,几乎只运行-delevent几次,直到它说什么都不存在。然后再次尝试安装它应该可以工作。

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

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

这是我的代码:

的Program.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.");
        }
    }
}
相关问题