C#Windows服务未启动
我尝试创建一个自动启动的windows服务。 我能够安装和卸载该服务。如果我尝试启动它,我会收到以下错误消息:“Der Dienst antwortete nicht rechtzeitig auf die start-order Steueranfrage”。(我试着翻译为)“服务在启动或控制请求时没有及时响应” 这是我可怜的代码C#Windows服务未启动,c#,service,windows-services,C#,Service,Windows Services,我尝试创建一个自动启动的windows服务。 我能够安装和卸载该服务。如果我尝试启动它,我会收到以下错误消息:“Der Dienst antwortete nicht rechtzeitig auf die start-order Steueranfrage”。(我试着翻译为)“服务在启动或控制请求时没有及时响应” 这是我可怜的代码 public class LisaServerService: System.ServiceProcess.ServiceBase { privat
public class LisaServerService: System.ServiceProcess.ServiceBase
{
private Program lisaServerServiceProgram;
public static string LisaServiceName = "LISA-ServerService";
[STAThread]
public static void Main(string[] args)
{
LisaServerService lisaServerService = new LisaServerService();
if (Environment.UserInteractive)
{
lisaServerService.OnStart(args);
Console.WriteLine("I am a service. Normally you can't see the console... just ignore me");
Console.ReadLine();
lisaServerService.OnStop();
}
else
{
ServiceBase.Run(lisaServerService);
}
}
public LisaServerService()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.CanShutdown = true;
this.ServiceName = "LISA - ServerService";
this.CanPauseAndContinue = true;
this.lisaServerServiceProgram = new Program();
}
protected override void OnStart(string[] args)
{
lisaServerServiceProgram.Start(null);
base.OnStart(args);
}
protected override void OnStop()
{
lisaServerServiceProgram.Stop();
base.OnStop();
}
protected override void OnShutdown()
{
OnStop();
base.OnShutdown();
}
}
Program.cs
public class Program
{
public Program()
{
Logger.LogLevel = LogLevel.Information;
Logger.LogRange = LogRange.Write;
Logger.Log("Logger initialized");
}
public void Start(string[] args)
{
DatabaseHandler.StartDatabase();
NetworkHandler.StartNetwork();
Logger.Log("Service started");
}
如果我以控制台程序的形式运行该服务,它可以正常工作。
因此,db连接+记录器也可以正常工作。(也在<10毫秒内)如果您在交互模式下运行服务,它将在此处等待控制台:
if (Environment.UserInteractive)
{
lisaServerService.OnStart(args);
Console.WriteLine("I am a service. Normally you can't see the console... just ignore me");
Console.ReadLine();
...
这可能会阻止服务正确响应以指示它已启动。程序的构造函数和LisaServerService的构造函数是什么样子的?我想说,您的OnStart方法永远不会返回。。是吗?事件查看器上有任何提示吗?可能运行服务的凭据不足以访问数据库?另外,我觉得最好将服务本身与主功能分开。您是否尝试过设置断点并查看它是否在某处引发异常?我删除了行,但结果相同