1

每次在运行 Windows 7 或 Windows Server 2008 R2 的系统上安装我的 Windows 服务(用 C# 开发,Visual Studio 2010)时,都会记录系统错误 7030。该服务未配置为交互式。我创建的自定义安装程序子类将帐户类型设置为 NetworkService:

private System.ServiceProcess.ServiceProcessInstaller _serviceProcessInstaller;
private System.ServiceProcess.ServiceInstaller _serviceInstaller;

public ProjectInstaller()
{
    InitializeComponent();
}

private void InitializeComponent()
{
    _serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
    _serviceInstaller = new System.ServiceProcess.ServiceInstaller();
    _serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.NetworkService;
    _serviceProcessInstaller.Password = null;
    _serviceProcessInstaller.Username = null;

    _serviceInstaller.ServiceName = Resources.ServiceName;
    _serviceInstaller.Description = Resources.ServiceDescription;
    _serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

    Installers.AddRange(new Installer[]
                            {
                               _serviceProcessInstaller,
                               _serviceInstaller
                            });
}

我也尝试将帐户类型设置为 LocalSystem,但在这种情况下也会记录错误 7030。

在同一个安装程序子类中,我还尝试根据如何在代码中配置我的 Windows 服务以访问桌面?

    /// <summary>
    /// Override OnCommitted() to overwrite the value of Interactive to be false.
    /// </summary>
    /// <remarks>
    /// See https://stackoverflow.com/questions/1945529/how-can-i-configure-my-windows-service-in-the-code-to-access-the-desktop
    /// </remarks>
    /// <param name="savedState"></param>
    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        const string pathToKey = @"SYSTEM\CurrentControlSet\services\MyService.exe";
        const string type = "Type";

        using (var registryKey = Registry.LocalMachine.OpenSubKey(pathToKey, true))
        {
            if (registryKey != null && registryKey.GetValue(type) != null)
            {
                registryKey.SetValue(type, (((int)registryKey.GetValue(type) & ~0x100)));
            }
        }

        base.OnCommitted(savedState);
    }

这两次修复尝试都没有奏效。我还在 SO 上查看了以下内容:“允许服务与桌面交互”的替代方案?以及如何在 Windows 服务安装程序中设置“与桌面交互”

4

0 回答 0