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

标签 c# visual-studio-2010 service windows-services console-application

我用了this tutorial使用 Visual Studio 2010 及其库存控制台应用程序项目创建 C# Windows 服务,但在我更改了所有内容并尝试按如下方式安装它之后:

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

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

Remove InstallState file because there are no installers.

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

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

    }
}

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

最佳答案

好的,我明白了。两个错误:

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

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

因此:

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

installutil的版本无关。

关于c# - 如何使用 Visual Studio 2010 将 Windows 控制台应用程序 C# 项目更改为 Windows 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15672447/

相关文章:

android - 不可阻挡的 Android 服务正在监听地理定位

Android: Activity 关闭时服务停止

c# - 二进制列表的 LINQ 三元结果

visual-studio-2010 - 在 Visual Studio 2010 中自定义选项卡大小

C# Visual Studio 2010 : make custom preset classes

c++ - 在 Visual Studio C++ 2010 中找不到或打开 PDB 文件

c# - 将json字符串反序列化为对象C#.net

c# - 为什么我不能在数组上调用 RemoveAt?

c# - 在具有 32 个以上逻辑内核的系统上使用 Process.ProcessorAffinity

asp.net-mvc-2 - ASP.NET MVC - 我想我做错了