Kas*_*sen 14 c# windows-services
我有一个我正在尝试调试的Windows服务.现在即使当前的代码工作也无法启动.错误是:
Windows无法在本地计算机上启动MyService服务
错误1053:服务未及时响应启动或控制请求.
为了隔离错误,我试图评论一切.主要方法如下:
TextWriter tt = new StreamWriter(@"C:\startup.text", true);
tt.WriteLine("Starting up the service");
tt.Close();
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
TextWriter tt2 = new StreamWriter(@"C:\startup.text", true);
tt2.WriteLine("Run...");
tt2.Close();
Run Code Online (Sandbox Code Playgroud)
它将"启动服务"和"运行..."打印到日志文件中.我也剥离了MyService的内部,所以它是空的.在任何代码周围都有一个try/catch,现在可以简化为上面的一些日志行.我从未输入将记录它的catch语句.
OnStart中的所有内容都已被注释掉:
protected override void OnStart(string[] args)
{
}
Run Code Online (Sandbox Code Playgroud)
所以我基本上没有想法.我认为错误是因为Start方法永远不会完成(或者不会在30秒内完成).是否有其他方法被调用?任何想法都表示赞赏.
额外信息: MyService中的构造函数为空.如果我插入一些Thread.Sleep(5000)行,则需要更长的时间才能弹出有关错误1053的错误消息.Main方法似乎必须退出(没有错误).
Dmi*_*try 14
您缺少ServiceBase.Run调用:
ServiceBase[] servicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(servicesToRun);
Run Code Online (Sandbox Code Playgroud)
订阅未处理的异常通知也可能是个好主意:
static void Main() {
...
AppDomain.CurrentDomain.UnhandledException
+= CurrentDomain_UnhandledException;
...
}
private static void CurrentDomain_UnhandledException(
Object sender,
UnhandledExceptionEventArgs e) {
if (e != null && e.ExceptionObject != null) {
// log exception:
}
}
Run Code Online (Sandbox Code Playgroud)
并将以下try/catch添加到OnStart,因为.NET/SCM吞下异常:
protected override void OnStart(String[] args) {
try {
} catch(Exception e) {
// log exception:
throw;
}
}
Run Code Online (Sandbox Code Playgroud)