【发布时间】:2018-01-14 22:38:35
【问题描述】:
我使用 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();
}
}
【问题讨论】:
-
见this question - 虽然我个人会使用Topshelf - “通过引用Topshelf,您的控制台应用程序成为一个具有全面命令集的服务安装程序-line 选项,用于安装、配置和运行您的应用程序作为服务。”
标签: c# visual-studio-2015 windows-services