我用了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);
}
}
}
那么我在这里做错了什么?
最佳答案
好的,我明白了。两个错误:
安装程序类必须声明为
public
前面必须有
[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/