我将控制台应用程序更改为使用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();
}
}
答案 0 :(得分:0)
我相信你需要从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);
}
}
答案 1 :(得分:0)
我最喜欢的安装服务的方法是使用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 post