You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB
C#

using ServiceDeamon;
using System.Configuration;
using System.ServiceProcess;
using Topshelf;
public class Program
{
public static void Main(string[] args)
{
try
{
var serviceName = ConfigurationManager.AppSettings["DeamonServiceName"];
var serviceDisplayName = ConfigurationManager.AppSettings["DeamonServiceDisplayName"];
if (Environment.UserInteractive)
{
Console.Title = serviceDisplayName;
}
Host host = HostFactory.New(x =>
{
// 基本的配置
x.RunAsLocalSystem();
x.SetServiceName(serviceName);
x.SetDisplayName(serviceDisplayName);
x.StartAutomaticallyDelayed();
x.EnableShutdown();
// 注册服务
x.Service(hostSettings => new DeamonService());
// 设置服务失败后的操作,分别对应第一次、第二次、后续
x.EnableServiceRecovery(t =>
{
t.RestartService(0);
t.RestartService(0);
t.RestartService(0);
t.OnCrashOnly();
});
});
host.Run();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}