[英]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.