如何使用“本地服务”代替“本地系统”?

时间:2012-10-16 09:38:03

标签: c# windows-services

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

  

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);
        }
    }
}

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

您使用的是哪种操作系统?

根据Microsoft

  

值LocalService和NetworkService仅在Windows XP和Windows Server 2003家族中可用。

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