[英]Console application as Windows service installation issue
我使用ServiceBase更改了控制台应用程序以用作Windows服务。 我使用以下命令安装了它。 但是我没有在服务中找到服务。 我检查了日志说
“在C:\\ Test \\ MyService.exe程序集中找不到带有RunInstallerAttribute.Yes属性的公共安装程序”
如何为控制台应用程序创建安装程序? 请告诉我。
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "c:\MyService.exe"
using System.ServiceProcess;
public static class Program
{
public static bool Cancelled { get; set; }
#region Nested classes to support running as service
public const string ServiceName = "MyService";
public class Service : ServiceBase
{
public Service()
{
ServiceName = Program.ServiceName;
}
protected override void OnStart(string[] args)
{
Program.Start(args);
}
protected override void OnStop()
{
Program.Stop();
}
}
#endregion
static void Main(string[] args)
{
if (!Environment.UserInteractive)
// running as service
using (var service = new Service())
ServiceBase.Run(service);
else
{
// running as console app
Start(args);
Console.WriteLine("Press any key to stop...");
Console.ReadKey(true);
Stop();
}
}
private static void Start(string[] args)
{
// onstart code here
try
{
SaveMessage();
}
catch (Exception e)
{
LogError();
}
}
private static void Stop()
{
// onstop code here
DisposeAll();
}
}
我相信您需要从System.Configuration.Install.Installer扩展
就像是
public class ServiceRegister: Installer
{
public ServiceRegister()
{
ServiceProcessInstaller serviceProcessInstaller =
new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
#if RUNUNDERSYSTEM
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
#else
// should prompt for user on install
processInstaller.Account = ServiceAccount.User;
processInstaller.Username = null;
processInstaller.Password = null;
#endif
serviceInstaller.DisplayName = "SomeName";
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "SomeName";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
我最喜欢的安装服务的方法是使用SC
命令行实用程序。
完整语法(吓到大家!)
sc [<ServerName>] create [<ServiceName>] [type= {own | share | kernel | filesys | rec | interact type= {own | share}}] [start= {boot | system | auto | demand | disabled}] [error= {normal | severe | critical | ignore}] [binpath= <BinaryPathName>] [group= <LoadOrderGroup>] [tag= {yes | no}] [depend= <dependencies>] [obj= {<AccountName> | <ObjectName>}] [displayname= <DisplayName>] [password= <Password>]
简单来说,
SC create YourServiceName start= auto binPath= "path/to/your/exe" DisplayName= "Your Display Name"
要删除服务,命令是
SC delete YourServiceName
以上命令需要在具有管理员权限的命令提示符下运行。 请注意,“ =”符号后的空格很重要。
相关SO 帖子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.