我正在使用Matt Davis的解决方案:How to make a .NET Windows Service start right after the installation?(第二个答案)以编程方式安装Windows服务。
但我不知道如何使用它。他放在Main
内的代码应该放在哪里?内部服务Main
,或它是控制台应用程序的main
?
如何启动/安装服务以及如何传递-install
个参数?
我试过了:
sc start myservice -install
但不起作用:(
它告诉我:
指定的服务不存在为已安装的服务
我非常感谢帮助,因为我正在努力使用这些服务
答案 0 :(得分:0)
我不知道你是如何创建Windows服务的,但是当我这样做时,我开始使用控制台应用程序。您将拥有Installer.cs和Program.cs类。安装程序类将在安装之前和之后进行必要的操作,并且Program类将执行一致的操作。
这里有一个可以使用的Installer.cs:
using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Reflection;
using System.Configuration.Install;
namespace MyWindowsService
{
[System.ComponentModel.RunInstallerAttribute(true)]
public class MyServiceInstaller : Installer
{
ServiceInstaller _serviceInstaller = new ServiceInstaller();
ServiceProcessInstaller _processInstaller = new ServiceProcessInstaller();
string _serviceName = "MyWindowsService";
string _displayName = "MyWindowsService";
string _description = "My Windows Service Application";
public MyServiceInstaller()
{
this.BeforeInstall += new InstallEventHandler(ProjectInstaller_BeforeInstall);
_processInstaller.Account = ServiceAccount.LocalSystem;
_serviceInstaller.StartType = ServiceStartMode.Automatic;
_serviceInstaller.Description = _description;
_serviceInstaller.ServiceName = _serviceName;
_serviceInstaller.DisplayName = _displayName;
Installers.Add(_serviceInstaller);
Installers.Add(_processInstaller);
}
/// <summary>
/// This function is called after installation completed
/// </summary>
/// <param name="savedState"></param>
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
ServiceController sc = new ServiceController(_serviceName);
// Run Windows service if it is not running
if (sc.Status != ServiceControllerStatus.Running)
{
sc.Start();
}
else
{
// Restart windows service if it is already running
RestartService(10000);
}
}
private void RestartService(int timeoutMiliseconds)
{
ServiceController service = new ServiceController(_serviceName);
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMiliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMiliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
void ProjectInstaller_BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());
foreach (ServiceController s in services)
{
if (s.ServiceName == this._serviceInstaller.ServiceName)
{
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
ServiceInstallerObj.Context = new System.Configuration.Install.InstallContext();
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = _serviceName;
ServiceInstallerObj.Uninstall(null);
}
}
}
}
}
这可以用作Program.cs:
namespace MyWindowsService
{
class Program : ServiceBase
{
static void Main()
{
System.ServiceProcess.ServiceBase.Run(new Program());
}
// This function is called after windows service starts running
protected override void OnStart(string[] args)
{
// Do something
base.OnStart(args);
}
// This function is called when windows service stopping
protected override void OnStop()
{
// Do something
base.OnStop();
}
}
}
此示例卸载先前安装的应用程序,再次安装并在安装后自动启动Windows服务。
这里还有link可以帮到你。