3

我使用 ServiceProcessInstaller 安装了我的 C# 服务(目前在 Windows 7 上;我读过它仅XP 和 2003 之后才存在,但它应该是我需要处理的全部内容)。我的服务可以在“本地系统”下运行,但不能在“本地服务”或“网络服务”下运行。即使我的服务基本上是空的(见下面的代码),我仍然得到

Windows 无法在本地计算机上启动 [服务名称] 服务。错误 5:访问被拒绝。

这是我的大部分空服务: 编辑我更新了代码以赋予“本地服务”必要的访问权限。

// Needs System.Configuration.Install.dll and System.ServiceProcess.dll
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;

namespace ServiceTest
{
    static class Constants
    {
    public const string LocalServiceAcctName = @"NT AUTHORITY\LOCAL SERVICE";
        public const string ServiceName = "MySvc";
    }

    class Program
    {
        /// <summary>
        /// Main entry point.
        /// </summary>
        /// <param name="args">
        ///     If the 1st argument is "i", it will simply install the service.
        ///     If it's something else, it will run as a Console application.
        ///     If it's empty, it will run as a service.
        /// </param>
        static void Main(string[] args)
        {
            if (args != null && args.Length != 0 && args[0] == "i")
            {
                bool result = InstallService(Assembly.GetExecutingAssembly().Location, Constants.ServiceName);
                Console.WriteLine(result ? "INSTALLED" : "ERROR INSTALLING");
                Console.ReadLine();
            }
            else
            {
                var host = new Host();
                bool runAsSvc = args == null || args.Length == 0;
                host.Launch(runAsSvc);
            }
        }

        static bool InstallService(string exeFullPath, string serviceName)
        {
            AssemblyInstaller installer = new AssemblyInstaller(exeFullPath, null);

            if (ServiceController.GetServices().Any(svcCtlr => svcCtlr.ServiceName == serviceName))
                installer.Uninstall(null);

            Hashtable dico = new Hashtable();
            installer.Install(dico);
            installer.Commit(dico);

        // Gives "Local Service" the necessary rights on the folder and subfolders and files.
        DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(exeFullPath));
        DirectorySecurity dirSec = dirInfo.GetAccessControl(AccessControlSections.Access);

        FileSystemRights rights = FileSystemRights.Modify;
        AccessControlType accessType = AccessControlType.Allow;
        dirSec.AddAccessRule(new FileSystemAccessRule(Constants.LocalServiceAcctName, rights, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, accessType));
        dirSec.AddAccessRule(new FileSystemAccessRule(Constants.LocalServiceAcctName, rights, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, accessType));
        dirSec.AddAccessRule(new FileSystemAccessRule(Constants.LocalServiceAcctName, rights, accessType));
        dirInfo.SetAccessControl(dirSec);


            return true;
        }
    }

    class Host
    {
        internal void Launch(bool runAsSvc)
        {
            if (runAsSvc)
                RuntimeService.CreateAndRun(this);
            else
            {
                OnStart();
                Console.WriteLine("Component started as a Console app.");
                Console.WriteLine("We work a lot...and then we're done.");
                OnStop();

                Console.WriteLine("Press enter to stop.");
                Console.ReadLine();
            }
        }

        internal void OnStart() { Console.WriteLine("We're starting!"); }

        internal void OnStop() { Console.WriteLine("We're stopping..."); }
    }

    class RuntimeService : ServiceBase
    {
        Host _host;

        public RuntimeService(Host host) { _host = host; }

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

        protected override void OnStop() { _host.OnStop(); }

        internal static void CreateAndRun(Host host) { ServiceBase.Run(new RuntimeService(host)); }
    }

    /// <summary>
    /// Class used to install the service.
    /// </summary>
    [RunInstaller(true)]
    public class RuntimeInstaller : Installer
    {
        public RuntimeInstaller()
        {
            var processInstaller = new ServiceProcessInstaller();
            processInstaller.Account = ServiceAccount.LocalService; // ServiceAccount.LocalSystem;

            var serviceInstaller = new ServiceInstaller();
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = Constants.ServiceName;

            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }
    }
}
4

2 回答 2

3

您在哪里安装了该服务?该错误似乎表明本地服务帐户无权访问服务位置。因此,服务管理器甚至无法加载可执行文件并使其运行,更不用说启动服务了。

我经常看到开发人员在开发/调试服务时这样做。他们从其中一个开发文件夹安装服务,但将其设置为在另一个帐户下运行。但默认情况下,只有用户、系统和管理员组有权访问用户的文件夹 - 因此服务失败并出现访问被拒绝错误。

于 2012-10-17T01:58:07.777 回答
0

您使用什么操作系统?

根据微软

LocalService 和 NetworkService 值仅适用于 Windows XP 和 Windows Server 2003 家族。

当我第一次在这里阅读此评论时,我认为它是在这些操作系统是最新版本时编写的,并且用于排除早期的操作系统,但此评论仍然存在于最新文档中。

于 2012-10-16T11:10:00.357 回答