如何使用Visual Studio 2010将Windows控制台应用程序C#项目更改为Windows服务?

ahm*_*md0 2 c# service windows-services console-application visual-studio-2010

我使用本教程使用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);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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

ahm*_*md0 5

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

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

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

因此:

namespace MySrvr
{
    [RunInstaller(true)]
    public class MyServiceInstaller : System.Configuration.Install.Installer
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

版本installutil与它无关.