在.NET中自行安装Windows服务

问题描述:

我已阅读此问题。我有同样的问题,但我不明白lubos hasko的答案。我到底该怎么做?

I have read this question. I have same issue, but I don't understand the answer from lubos hasko. How exactly can I do it? Can you someone post me full walkthrough?

当我在下面运行代码时,已经安装了某些东西,但是在服务列表中找不到它。

When I run code below, something is installed, but in list of service, I could not find it.

我有这个,但这不起作用:

I have this, but this not work:

using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{

public class Service1 : ServiceBase
{
    public Service1()
    {
        File.AppendAllText("sss.txt", "ccccc");
    }

    protected override void OnStart(string[] args)
    {
        File.AppendAllText("sss.txt", "asdfasdf");
    }

    protected override void OnStop()
    {
        File.AppendAllText("sss.txt", "bbbbb");
    }


    static void Main(string[] args)
    {
        if (System.Environment.UserInteractive)
        {
            string parameter = string.Concat(args);
            switch (parameter)
            {
                case "--install":
                    ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                    break;
                case "--uninstall":
                    ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                    break;
            }
        }
        else
        {
            ServiceBase.Run(new Service1());
        }


        Console.ReadKey();
    }
 }
}

我也不奇怪:

if (System.Environment.UserInteractive) ...


这是我完整的解决方案,并且可以使用。基本上与问题中的答案相同。

This is my complete solution, and it works. It is basically the same answer as in this question.

using System;
using System.Configuration.Install;
using System.Reflection;
using System.ServiceProcess;
using System.IO;

namespace ConsoleApplication1
{
    class Program : ServiceBase
    {
        static void Main(string[] args)
        {

            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;


            if (System.Environment.UserInteractive)
            {
                string parameter = string.Concat(args);
                switch (parameter)
                {
                    case "--install":
                        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                        break;
                    case "--uninstall":
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                        break;
                }
            }
            else
            {
                ServiceBase.Run(new Program());
            }



        }

        private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            File.AppendAllText(@"C:\Temp\error.txt", ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message);
        }

        public Program()
        {
            this.ServiceName = "My Service";
            File.AppendAllText(@"C:\Temp\sss.txt", "aaa");

        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            File.AppendAllText(@"C:\Temp\sss.txt", "bbb");
        }

        protected override void OnStop()
        {
            base.OnStop();

            File.AppendAllText(@"C:\Temp\sss.txt", "ccc");
        }
    }
}

,并在同一项目中创建类:

and in same project create this class:

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

namespace ConsoleApplication1
{
    [RunInstaller(true)]
    public class MyWindowsServiceInstaller : Installer
    {
        public MyWindowsServiceInstaller()
        {
            var processInstaller = new ServiceProcessInstaller();
            var serviceInstaller = new ServiceInstaller();

            //set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;

            serviceInstaller.DisplayName = "My Service";
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            //must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = "My Service";
            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}

使用参数运行该程序- -install /-在Windows 7上以管理员身份卸载。检查temp中的错误日志。在同一路径上检查工作日志。

Run this program with parameters --install/--uninstall as Administrator on Windows 7. Check the error log in temp. Check working log on the same path.