c# - 如何使用 "Local Service"而不是 "Local System"?

标签 c# windows-services

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

Windows could not start the [service name] service on Local Computer. Error 5: Access is denied.

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

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

最佳答案

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

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

关于c# - 如何使用 "Local Service"而不是 "Local System"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911679/

相关文章:

c# - 异步方法调用期间主线程完成?

c# - 如何验证 Windows 服务是否正在运行

multithreading - 向 sleep 或阻塞线程发出信号以停止的最佳方法是什么?

.net - 如何在 Windows 服务上捕获未处理的异常?

c# - 如何将 LinqToSql Table<TEntity> 转换为 Table<IEntity> where TEntity : IEntity?

c# - Action 需要 Swagger 的独特方法/路径组合

c# - 如何使以下C#GroupBy功能更好?

C# 使用 : constructor in a different method

c# - 客户端在 Windows 服务中找不到 WCF 的端点元素

c# windows 服务还是普通程序?