Windows服务安装与控制

Windows服务安装与控制

1、建立服务

  (1)定义一个ServiceInstaller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowService
{
    [System.ComponentModel.RunInstaller(true)]
    public class ServiceInstaller : System.Configuration.Install.Installer
    {
        System.ServiceProcess.ServiceProcessInstaller processInstaller;
        System.ServiceProcess.ServiceInstaller serviceInstaller;
        public ServiceInstaller()
        {
            processInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            Installers.Add(processInstaller);

            serviceInstaller = new System.ServiceProcess.ServiceInstaller();
            serviceInstaller.DisplayName = "000000000";
            serviceInstaller.ServiceName = "000000000";
            serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

            Installers.Add(serviceInstaller);
        }
        protected override void OnAfterInstall(System.Collections.IDictionary savedState)
        {
            base.OnAfterInstall(savedState);

        }
    }
}

(2)定义一个服务

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowService
{
    partial class Service : ServiceBase
    {
        public Service()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            // TODO:  在此处添加代码以启动服务。
        }

        protected override void OnStop()
        {
            // TODO:  在此处添加代码以执行停止服务所需的关闭操作。
        }
    }
}

(3)定义自己的安装方式:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;

namespace WindowService
{
    public class ServiceMaster
    {
        static string appPath = Assembly.GetExecutingAssembly().Location;
        public static void Main(string[] args)
        {
            if (args != null && args.Length > 0)
            {
                if (args[0] == "-i")
                {
                    InstallService();
                }
                else if (args[0] == "-u")
                {
                    UninstallService();
                }

            }
            else
            {
                var s = new Service() { ServiceName = "aaaaa" };
                Service.Run(s);
            }
        }
        public static void InstallService()
        {
            System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { appPath });
        }

        public static void UninstallService()
        {
            System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "-u", appPath });
        }
    }
}

安装时,使用管理员权限运行cmd,输入程序带参数"-i"执行即可安装。这是使用的ManagedInstallerClass类的安装。接下来对上面这个服务使用另一种安装方式。

2、Windows Api安装方式。

(1)辅助类的设计

using System;
using System.ComponentModel;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace Yihe.Service
{
    #region 异常定义

    /// <summary>
    /// 服务不存在异常
    /// </summary>
    public class ServiceNotExistException : ApplicationException
    {
        public ServiceNotExistException() : base("服务不存在!") { }

        public ServiceNotExistException(string message) : base(message) { }
    }

    #endregion

    #region 枚举定义

    /// <summary>
    /// 服务启动类型
    /// </summary>
    public enum ServiceStartType
    {
        Boot,
        System,
        Auto,
        Manual,
        Disabled
    }

    /// <summary>
    /// 服务运行帐户
    /// </summary>
    public enum ServiceAccount
    {
        LocalSystem,
        LocalService,
        NetworkService,
    }

    #endregion

    /// <summary>
    /// Windows 服务辅助类
    /// </summary>
    public static class ServiceHelper
    {
        #region 公开方法

        /// <summary>
        /// 安装服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        /// <param name="displayName">友好名称</param>
        /// <param name="binaryFilePath">映像文件路径,可带参数</param>
        /// <param name="description">服务描述</param>
        /// <param name="startType">启动类型</param>
        /// <param name="account">启动账户</param>
        /// <param name="dependencies">依赖服务</param>
        public static void Install(string serviceName, string displayName, string binaryFilePath, string description, ServiceStartType startType, ServiceAccount account = ServiceAccount.LocalSystem, string[] dependencies = null)
        {
            IntPtr scm = OpenSCManager();

            IntPtr service = IntPtr.Zero;
            try
            {

                service = Win32Class.CreateService(scm, serviceName, displayName, Win32Class.SERVICE_ALL_ACCESS, Win32Class.SERVICE_WIN32_OWN_PROCESS, startType, Win32Class.SERVICE_ERROR_NORMAL, binaryFilePath, null, IntPtr.Zero, ProcessDependencies(dependencies), GetServiceAccountName(account), null);

                if (service == IntPtr.Zero)
                {
                    if (Marshal.GetLastWin32Error() == 0x431)//ERROR_SERVICE_EXISTS
                    { throw new ApplicationException("服务已存在!"); }

                    throw new ApplicationException("服务安装失败!");
                }

                //设置服务描述
                Win32Class.SERVICE_DESCRIPTION sd = new Win32Class.SERVICE_DESCRIPTION();
                try
                {
                    sd.description = Marshal.StringToHGlobalUni(description);
                    Win32Class.ChangeServiceConfig2(service, 1, ref sd);
                }
                finally
                {
                    Marshal.FreeHGlobal(sd.description); //释放
                }
            }
            finally
            {
                if (service != IntPtr.Zero)
                {
                    Win32Class.CloseServiceHandle(service);
                }
                Win32Class.CloseServiceHandle(scm);
            }
        }

        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        public static void Uninstall(string serviceName)
        {
            IntPtr scmHandle = IntPtr.Zero;
            IntPtr service = IntPtr.Zero;

            try
            {
                service = OpenService(serviceName, out scmHandle);

                StopService(service); //停止服务。里面会递归停止从属服务

                if (!Win32Class.DeleteService(service) && Marshal.GetLastWin32Error() != 0x430) //忽略已标记为删除的服务。ERROR_SERVICE_MARKED_FOR_DELETE
                {
                    throw new ApplicationException("删除服务失败!");
                }
            }
            catch (ServiceNotExistException) { } //忽略服务不存在的情况
            finally
            {
                if (service != IntPtr.Zero)
                {
                    Win32Class.CloseServiceHandle(service);
                    Win32Class.CloseServiceHandle(scmHandle);//放if里面是因为如果服务打开失败,在OpenService里就已释放SCM
                }
            }
        }

        #endregion

        #region 辅助方法

        /// <summary>
        /// 转换帐户枚举为有效参数
        /// </summary>
        public static string GetServiceAccountName(ServiceAccount account)
        {
            if (account == ServiceAccount.LocalService)
            {
                return @"NT AUTHORITY\LocalService";
            }
            if (account == ServiceAccount.NetworkService)
            {
                return @"NT AUTHORITY\NetworkService";
            }
            return null;
        }

        /// <summary>
        /// 处理依赖服务参数
        /// </summary>
        public static string ProcessDependencies(string[] dependencies)
        {
            if (dependencies == null || dependencies.Length == 0)
            {
                return null;
            }

            StringBuilder sb = new StringBuilder();
            foreach (string s in dependencies)
            {
                sb.Append(s).Append(‘\0‘);
            }
            sb.Append(‘\0‘);

            return sb.ToString();
        }

        #endregion

        #region API 封装方法

        /// <summary>
        /// 打开服务管理器
        /// </summary>
        public static IntPtr OpenSCManager()
        {
            IntPtr scm = Win32Class.OpenSCManager(null, null, Win32Class.SC_MANAGER_ALL_ACCESS);

            if (scm == IntPtr.Zero)
            {
                throw new ApplicationException("打开服务管理器失败!");
            }

            return scm;
        }

        /// <summary>
        /// 打开服务
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <param name="scmHandle">服务管理器句柄。供调用者释放</param>
        public static IntPtr OpenService(string serviceName, out IntPtr scmHandle)
        {
            scmHandle = OpenSCManager();

            IntPtr service = Win32Class.OpenService(scmHandle, serviceName, Win32Class.SERVICE_ALL_ACCESS);

            if (service == IntPtr.Zero)
            {
                int errCode = Marshal.GetLastWin32Error();

                Win32Class.CloseServiceHandle(scmHandle); //关闭SCM

                if (errCode == 0x424) //ERROR_SERVICE_DOES_NOT_EXIST
                {
                    throw new ServiceNotExistException();
                }

                throw new Win32Exception();
            }

            return service;
        }
        private static bool ExistService(string serviceName)
        {
            IntPtr scmHandle = IntPtr.Zero;
            IntPtr handle = IntPtr.Zero;
            try
            {
                handle = OpenService(serviceName, out scmHandle);
                if (handle == IntPtr.Zero)
                {
                    int errCode = Marshal.GetLastWin32Error();
                    if (errCode == 0x424)
                    { return false; }
                }
            }
            catch (ServiceNotExistException e) { return false; }
            finally
            {
                Win32Class.CloseServiceHandle(scmHandle); //关闭SCM
                Win32Class.CloseServiceHandle(handle); //关闭服务
            }
            return true;
        }
        public static bool StartService(string serviceName, TimeSpan timeSpan)
        {
            if (!ExistService(serviceName)) return false;
            System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(serviceName);
            if (sc.Status != System.ServiceProcess.ServiceControllerStatus.Running && sc.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
            {
                sc.Start();
            }
            sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, timeSpan);
            return sc.Status == System.ServiceProcess.ServiceControllerStatus.Running;
        }

        public static bool StopService(string serviceName, TimeSpan timeSpan)
        {
            if (!ExistService(serviceName)) return false;
            System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(serviceName);
            if (sc.Status != System.ServiceProcess.ServiceControllerStatus.Stopped && sc.Status != System.ServiceProcess.ServiceControllerStatus.StopPending)
            {
                sc.Stop();
            }
            sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped, timeSpan);
            var isok = sc.Status == System.ServiceProcess.ServiceControllerStatus.Stopped;
            sc.Close();
            sc.Dispose();
            return isok;
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        public static void StopService(IntPtr service)
        {
            ServiceState currState = GetServiceStatus(service);

            if (currState == ServiceState.Stopped)
            {
                return;
            }

            if (currState != ServiceState.StopPending)
            {
                //递归停止从属服务
                string[] childSvs = EnumDependentServices(service, EnumServiceState.Active);
                if (childSvs.Length != 0)
                {
                    IntPtr scm = OpenSCManager();
                    try
                    {
                        foreach (string childSv in childSvs)
                        {
                            StopService(Win32Class.OpenService(scm, childSv, Win32Class.SERVICE_STOP));
                        }
                    }
                    finally
                    {
                        Win32Class.CloseServiceHandle(scm);
                    }
                }

                Win32Class.SERVICE_STATUS status = new Win32Class.SERVICE_STATUS();
                Win32Class.ControlService(service, Win32Class.SERVICE_CONTROL_STOP, ref status); //发送停止指令
            }

            if (!WaitForStatus(service, ServiceState.Stopped, new TimeSpan(0, 0, 30)))
            {
                throw new ApplicationException("停止服务失败!");
            }
        }

        /// <summary>
        /// 遍历从属服务
        /// </summary>
        /// <param name="serviceHandle"></param>
        /// <param name="state">选择性遍历(活动、非活动、全部)</param>
        public static string[] EnumDependentServices(IntPtr serviceHandle, EnumServiceState state)
        {
            int bytesNeeded = 0; //存放从属服务的空间大小,由API返回
            int numEnumerated = 0; //从属服务数,由API返回

            //先尝试以空结构获取,如获取成功说明从属服务为空,否则拿到上述俩值
            if (Win32Class.EnumDependentServices(serviceHandle, state, IntPtr.Zero, 0, ref bytesNeeded, ref numEnumerated))
            {
                return new string[0];
            }
            if (Marshal.GetLastWin32Error() != 0xEA) //仅当错误值不是大小不够(ERROR_MORE_DATA)时才抛异常
            {
                throw new Win32Exception();
            }

            //在非托管区域创建指针
            IntPtr structsStart = Marshal.AllocHGlobal(new IntPtr(bytesNeeded));
            try
            {
                //往上述指针处塞存放从属服务的结构组,每个从属服务是一个结构
                if (!Win32Class.EnumDependentServices(serviceHandle, state, structsStart, bytesNeeded, ref bytesNeeded, ref numEnumerated))
                {
                    throw new Win32Exception();
                }

                string[] dependentServices = new string[numEnumerated];
                int sizeOfStruct = Marshal.SizeOf(typeof(Win32Class.ENUM_SERVICE_STATUS)); //每个结构的大小
                long structsStartAsInt64 = structsStart.ToInt64();
                for (int i = 0; i < numEnumerated; i++)
                {
                    Win32Class.ENUM_SERVICE_STATUS structure = new Win32Class.ENUM_SERVICE_STATUS();
                    IntPtr ptr = new IntPtr(structsStartAsInt64 + i * sizeOfStruct); //根据起始指针、结构次序和结构大小推算各结构起始指针
                    Marshal.PtrToStructure(ptr, structure); //根据指针拿到结构
                    dependentServices[i] = structure.serviceName; //从结构中拿到服务名
                }

                return dependentServices;
            }
            finally
            {
                Marshal.FreeHGlobal(structsStart);
            }
        }

        /// <summary>
        /// 获取服务状态
        /// </summary>
        public static ServiceState GetServiceStatus(IntPtr service)
        {
            Win32Class.SERVICE_STATUS status = new Win32Class.SERVICE_STATUS();

            if (!Win32Class.QueryServiceStatus(service, ref status))
            {
                throw new ApplicationException("获取服务状态出错!");
            }

            return status.currentState;
        }

        /// <summary>
        /// 等候服务至目标状态
        /// </summary>
        public static bool WaitForStatus(IntPtr serviceHandle, ServiceState desiredStatus, TimeSpan timeout)
        {
            DateTime startTime = DateTime.Now;

            while (GetServiceStatus(serviceHandle) != desiredStatus)
            {
                if (DateTime.Now - startTime > timeout) { return false; }

                Thread.Sleep(200);
            }

            return true;
        }

        #endregion

        #region 嵌套类

        /// <summary>
        /// Win32 API相关
        /// </summary>
        private static class Win32Class
        {
            #region 常量定义

            /// <summary>
            /// 打开服务管理器时请求的权限:全部
            /// </summary>
            public const int SC_MANAGER_ALL_ACCESS = 0xF003F;

            /// <summary>
            /// 服务类型:自有进程类服务
            /// </summary>
            public const int SERVICE_WIN32_OWN_PROCESS = 0x10;

            /// <summary>
            /// 打开服务时请求的权限:全部
            /// </summary>
            public const int SERVICE_ALL_ACCESS = 0xF01FF;

            /// <summary>
            /// 打开服务时请求的权限:停止
            /// </summary>
            public const int SERVICE_STOP = 0x20;

            /// <summary>
            /// 服务操作标记:停止
            /// </summary>
            public const int SERVICE_CONTROL_STOP = 0x1;

            /// <summary>
            /// 服务出错行为标记
            /// </summary>
            public const int SERVICE_ERROR_NORMAL = 0x1;

            #endregion

            #region API所需类和结构定义

            /// <summary>
            /// 服务状态结构体
            /// </summary>
            [StructLayout(LayoutKind.Sequential)]
            public struct SERVICE_STATUS
            {
                public int serviceType;
                public ServiceState currentState;
                public int controlsAccepted;
                public int win32ExitCode;
                public int serviceSpecificExitCode;
                public int checkPoint;
                public int waitHint;
            }

            /// <summary>
            /// 服务描述结构体
            /// </summary>
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct SERVICE_DESCRIPTION
            {
                public IntPtr description;
            }

            /// <summary>
            /// 服务状态结构体。遍历API会用到
            /// </summary>
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public class ENUM_SERVICE_STATUS
            {
                public string serviceName;
                public string displayName;
                public int serviceType;
                public int currentState;
                public int controlsAccepted;
                public int win32ExitCode;
                public int serviceSpecificExitCode;
                public int checkPoint;
                public int waitHint;
            }

            #endregion

            #region API定义

            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern bool ChangeServiceConfig2(IntPtr serviceHandle, uint infoLevel, ref SERVICE_DESCRIPTION serviceDesc);

            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern IntPtr OpenSCManager(string machineName, string databaseName, int dwDesiredAccess);

            [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            public static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, int dwDesiredAccess);
            [DllImport("advapi32.dll")]
            public static extern int StartService(IntPtr SVHANDLE, int dwNumServiceArgs, string lpServiceArgVectors);
            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern IntPtr CreateService(IntPtr hSCManager, string lpServiceName, string lpDisplayName, int dwDesiredAccess, int dwServiceType, ServiceStartType dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);

            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern bool CloseServiceHandle(IntPtr handle);

            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern bool QueryServiceStatus(IntPtr hService, ref SERVICE_STATUS lpServiceStatus);

            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern bool DeleteService(IntPtr serviceHandle);

            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern bool ControlService(IntPtr hService, int dwControl, ref SERVICE_STATUS lpServiceStatus);

            [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern bool EnumDependentServices(IntPtr serviceHandle, EnumServiceState serviceState, IntPtr bufferOfENUM_SERVICE_STATUS, int bufSize, ref int bytesNeeded, ref int numEnumerated);

            #endregion
        }

        #endregion

        /// <summary>
        /// 服务状态枚举。用于遍历从属服务API
        /// </summary>
        public enum EnumServiceState
        {
            Active = 1,
            InActive = 2,
            All = 3
        }

        /// <summary>
        /// 服务状态
        /// </summary>
        public enum ServiceState
        {
            Stopped = 1,
            StartPending = 2,
            StopPending = 3,
            Running = 4,
            ContinuePending = 5,
            PausePending = 6,
            Paused = 7
        }
    }
}

(2)测试例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Yihe.Service
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //安装服务
            ServiceHelper.Install("aaaaa", "aaaaa", Application.StartupPath + "\\WindowService.exe", "无描述", ServiceStartType.Auto);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //卸载服务
            ServiceHelper.Uninstall("aaaaa");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //启动服务
            var isok = ServiceHelper.StartService("aaaaa", TimeSpan.FromSeconds(30));
            MessageBox.Show("是否成功:" + isok.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //停止服务
            var isok = ServiceHelper.StopService("aaaaa", TimeSpan.FromSeconds(30));
            MessageBox.Show("是否成功:" + isok.ToString());
        }
    }
}
时间: 2025-01-07 17:24:30

Windows服务安装与控制的相关文章

windows服务安装及卸载

1)安装脚本Install.bat%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe JobSchedule.exeNet Start ServiceOAsc config ServiceOA start= auto 2)卸载脚本Uninstall.bat%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u JobSchedule.exe w

C#编写的windows服务安装后启动提示“服务启动后又停止了”

使用C#编写的windows服务安装到服务器上行后进行启动时,总是提示“服务启动后又停止了”. 检查了服务逻辑是没问题,安装在开发本地也是正常,网上查了资料说是可能是服务没有注册,我检查了服务是正常注册,相对应的方法试很多了,但是都没有解决.后来无意中看了一个帖子说可以在windows的本地服务日志里边看报错信息.看到这个,我的问题就有办法处理了,查了一下保存信息,提示找不到“E:\\”,看到这里我就明白是怎么回事了,我的开发机有E盘,服务器上没有E盘,而我的日志文件默认写在E盘,所以服务启动后

2.Windows服务--&gt;安装卸载服务

1.使用vs组件“VS2012开发人员命令提示” 工具,进行安装卸载服务(必须以“管理员身份运行") 安装和卸载的时候选择合适的安装程序工具地址,例如: 安装服务:C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe 服务路径 卸载服务:C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u 服务路径 例如: C:\Windows\Microsoft.N

windows服务安装启动报错误1053:服务没有及时响应启动或控制请求

1 <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup> 2 3 </configuration> 用.net 开发了一个C#语言的windows服务,在本地和测试环境,安装启动都正常,在新的线上环境报错,不能启动-报出-错误1053:服务没有及时响应启动或控制请求. 后来发现时线上.NET FRAM

C#实现对Windows 服务安装

Windows服务作用:定时用户消息推送,WEB程序实时统计等 Windows服务创建:C#创建服务的方式也有很多种,建议大家在做之前可以先全面了解一下这方面东西再去动手这样便于解决中间遇到一些比较棘手的小问题. 主要说一种通过SC命令实现服务的创建.修改.查询.卸载.开始.暂停,具体服务工作的业务可以另行完善. 1,创建一个控制台程序,当然也可以写个winform或者其他XXX 2,在创建好的项目中新建一个服务 创建完服务,剩下的就需要代码实现了. 思路:我们将通过模拟在命令窗中输入SC服务命

windows 服务 安装、卸载

1.新建项目 选中windows服务 2.添加安装程序 3.修改安装代码 ServiceProcessInstaller processInstall; ServiceInstaller serviceInstall; public ProjectInstaller() { this.processInstall = new ServiceProcessInstaller(); this.serviceInstall = new ServiceInstaller(); processInstal

C#版Windows服务安装卸载小工具-附源码

前言 在我们的工作中,经常遇到Windows服务的安装和卸载,在之前公司也普写过一个WinForm程序选择安装路径,这次再来个小巧灵活的控制台程序,不用再选择,只需放到需要安装服务的目录中运行就可以实现安装或卸载. 开发思路 1.由于系统的权限限制,在运行程序时需要以管理员身份运行 2.因为需要实现安装和卸载两个功能,在程序运行时提示本次操作是安装还是卸载  需要输入 1 或 2 3.接下来程序会查找当前目录中的可执行文件并过滤程序本身和有时我们复制进来的带有vhost的文件,并列出列表让操作者

C#开发的Windows服务安装时需要“设置服务登录”

C#开发的Windows服务在安装的过程中会弹出一个"设置服务登录"对话框,要求输入用户名和密码.此时用户名和密码的形式必须以域用户的形式输入,如果没有加入域,用户名要用.\username 的格式,否则会提示错误. 注:需要设置服务登录,是因为serviceProcessInstaller的Account设置为User了,修改为LocalService安装Windows服务就不需要设置账号密码了.

Zookeeper以Windows服务安装运行

1.下载的Zookeeper是.cmd的批处理命令运行的,默认没有提供以windows服务的方式运行的方案 下载地址:http://zookeeper.apache.org/ 2.下载prunsrv 下载地址:http://archive.apache.org/dist/commons/daemon/binaries/windows/ 3.解压后复制文件 64位机器用amd64/prunsrv.exe  a. 复制 commons-daemon-1.0.15-bin-windows/amd64/