我使用Visual Studio 2012开发了一个Windows服务,我想在我的机器上安装它。
服务代码:
public partial class OpenTableImporter : ServiceBase
{
public OpenTableImporter()
{
InitializeComponent();
EventLog.Log = "OpenTableImporterTableLog";
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("The service was started successfully.", EventLogEntryType.Information);
XMLReader xmlReader = new XMLReader();
}
protected override void OnStop()
{
EventLog.WriteEntry("The service was stopped successfully.", EventLogEntryType.Information);
}
这是程序main的代码:
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new OpenTableImporter()
};
ServiceBase.Run(ServicesToRun);
}
}
这是日志:
Installing assembly 'XXX\ImporterOpenTable.exe'.
Affected parameters are:
logtoconsole =
logfile = XXX\ImporterOpenTable.InstallLog
assemblypath = XXX\ImporterOpenTable.exe
Installing service OpenTableImporter...
Service OpenTableImporter has been successfully installed.
Creating EventLog source OpenTableImporter in log OpenTableImporterTableLog...
Committing assembly 'XXX\ImporterOpenTable.exe'.
Affected parameters are:
logtoconsole =
logfile = XXX\ImporterOpenTable.InstallLog
assemblypath = XXX\ImporterOpenTable.exe
我已经:
Startup Object
更改为我的计划。 NETWORK SERVICE
读/写权限。program.cs
课程视为公开课程,然后不公开。答案 0 :(得分:0)
要实际安装该服务,您必须向服务控制管理器注册它。最简单的方法是使用SC.EXE
。
示例:
SC.EXE create "MyService" binpath= "\"c:\program files\MyService\MyService.exe\" arg1 arg2" displayname= "My Service Name"
这会创建一个服务,并告诉服务控制管理器启动该服务,它应该调用"c:\program files\MyService\MyService.exe"
并在命令行上传递两个参数arg1
和arg2
。例如,如果这些方案有/debug
和/service
选项。
如果您的可执行服务不需要任何特殊参数来运行该服务,您可以省略它们。
注意:如果可执行文件的路径包含空格,则需要使用反斜杠双引号引用它,即路径开头和结尾的\"
为我有我的例子。