2

我使用本教程使用 Visual Studio 2010 及其股票控制台应用程序项目创建 C# Windows 服务,但是在我更改了所有内容并尝试安装它之后:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil" /i myservice.exe

我在控制面板的服务列表中没有看到我的服务。然后我检查了输出installutil并发现了这条消息:

删除 InstallState 文件,因为没有安装程序。

我不确定它为什么这么说,因为我确实有一个安装程序类,定义如下:

namespace MySrvr
{
    class MyServiceInstaller : System.Configuration.Install.Installer
    {
        public MyServiceInstaller()
        {
            ServiceProcessInstaller process = new ServiceProcessInstaller();

            process.Account = ServiceAccount.LocalSystem;

            ServiceInstaller serviceAdmin = new ServiceInstaller();

            serviceAdmin.StartType = ServiceStartMode.Automatic;

            serviceAdmin.ServiceName = "MyServiceName";
            serviceAdmin.DisplayName = "My Service Display Name";
            serviceAdmin.Description = "My Service Description";

            Installers.Add(process);
            Installers.Add(serviceAdmin);
        }

    }
}

那么我在这里做错了什么?

4

1 回答 1

5

好,我知道了。两个错误:

  1. 安装程序类必须声明为public

  2. [RunInstaller(true)]它前面必须有属性。

像这样:

namespace MySrvr
{
    [RunInstaller(true)]
    public class MyServiceInstaller : System.Configuration.Install.Installer
    {
    }
}

的版本installutil与它无关。

于 2013-03-28T03:26:23.287 回答