无法将参数传递给服务(从代码安装服务)
本文关键字:服务 代码 安装 参数传递 | 更新日期: 2023-09-27 17:55:32
我正在使用Matt Davis的这个解决方案:如何使.NET Windows服务在安装后立即启动?(第二个答案)以编程方式安装 Windows 服务。但我不知道如何使用它。他放在里面的代码应该去哪里Main
?在服务Main
内部,还是控制台应用程序的main
?
如何启动/安装服务以及如何传递该-install
参数?
我试过这个:
sc start myservice -install
但不起作用:(
它告诉我:
指定的服务不作为已安装的服务存在
我真的很感激帮助,因为我正在努力获得这些服务
我不知道您是如何创建Windows服务的,但是当我这样做时,我从控制台应用程序开始。您将有安装程序.cs和程序.cs类。安装程序类将在安装前后进行必要的操作,程序类将执行一致的操作。
这里有一个安装程序.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);
}
}
}
}
}
这可以用作程序.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 服务。
这里还有一个链接,可以帮助您。