我已经创建并安装了几次服务.最初它工作正常,但在服务代码中的一些更改后,当我在Services.msc中重新启动服务时,它开始给出错误:
错误1053:服务未及时响应启动或控制请求
码:
public partial class AutoSMS : ServiceBase { public AutoSMS() { InitializeComponent(); eventLog1.Clear(); if (!System.Diagnostics.EventLog.SourceExists("MySource")) { System.Diagnostics.EventLog.CreateEventSource( "MySource", "MyNewLog"); } eventLog1.Source = "MySource"; eventLog1.Log = "MyNewLog"; Timer checkForTime = new Timer(5000); checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed); checkForTime.Enabled = true; } protected override void OnStart(string[] args) { eventLog1.WriteEntry("In OnStart"); } protected override void OnStop() { eventLog1.WriteEntry("In onStop."); } void checkForTime_Elapsed(object sender, ElapsedEventArgs e) { string Time = "15:05:00"; DateTime dateTime = DateTime.ParseExact(Time, "HH:mm:ss", CultureInfo.InvariantCulture); if (DateTime.Now == dateTime) ; eventLog1.WriteEntry(Time); } }
这是我的主要方法代码
static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new AutoSMS() }; ServiceBase.Run(ServicesToRun); }
我还尝试了以下步骤:
转到"开始">"运行">,然后键入regedit
导航到:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
选择控制文件夹后,右键单击右侧的窗格,然后 - 选择新的DWORD值
将新DWORD命名为:ServicesPipeTimeout
右键单击"ServicesPipeTimeout",然后单击"修改"
单击"十进制",键入"180000",然后单击"确定"
重新启动计算机
我曾经使用以下命令安装和卸载它:
installutil AutoSMS.exe installutil /u AutoSMS.exe
Pranav Singh.. 26
就我而言,我在调试模式下发布服务.
解决方案是:
将解决方案更改为发布模式
使用命令卸载旧服务 InstallUtil -u WindowsServiceName.exe
再次安装服务 InstallUtil -i WindowsServiceName.exe
它之后运作得很好.
我刚刚在.Net 4.5中尝试了本地代码,服务正常启动和停止.我怀疑你的问题可能在于创建EventLog源码.
方法:
EventLog.SourceExists("MySource")
要求运行代码的用户必须是管理员,根据此处的文档:
http://msdn.microsoft.com/en-us/library/x7y6sy21(v=vs.110).aspx
检查该服务是否以具有管理员权限的用户身份运行.
我遇到了同样的问题,并不确定如何解决它.是的,这是因为服务引发了异常,但您可以遵循一些通用准则来纠正此问题:
检查您是否已编写正确的代码以启动该服务:
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WinsowsServiceToRun()
};
ServiceBase.Run(ServicesToRun);
您需要确保在WinsowsServiceToRun类中运行某种无限循环
最后,可能有一些代码没有记录任何内容并突然关闭程序(我就是这种情况),在这种情况下,你将不得不按照旧的调试学校来编写一行来源(文本/分贝/哪里).我面临的问题是,由于运行该服务的帐户不是"管理员",因此代码刚刚下降并且没有记录任何异常,以防它尝试写入"Windows事件日志",即使代码是用于记录异常.记录到偶数日志实际上不需要管理员权限,但需要定义源.如果事件的源尚未在系统中定义,并且服务尝试在没有管理员权限的情况下首次记录它,则它将失败.要解决此问题,请按照以下步骤
使用admin权限打开命令提示符
粘贴命令: eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO <<Source>> /D "<<SourceUsingWhichToWrite>>"
按enter键
现在开始服务
在这个问题上花了太多时间之后。EventLog
尽管使用得当,但我发现了所有混乱的原因。
无论谁解决此问题,我都建议您摆脱EventLog
。使用更好的工具,例如“ log4net ”。
正如其他人所指出的那样,这个错误可能有很多原因.但是希望这会帮助别人,我将分享我们案件中发生的事情.对我们来说,我们的服务已升级到.NET 4.5,但服务器没有安装.NET 4.5.
就我而言,我在调试模式下发布服务.
解决方案是:
将解决方案更改为发布模式
使用命令卸载旧服务 InstallUtil -u WindowsServiceName.exe
再次安装服务 InstallUtil -i WindowsServiceName.exe
它之后运作得很好.
花了一些时间在问题上,尝试了无效的解决方案后,我遇到了这个博客。建议将服务初始化代码包装在try / catch块中,像这样,并添加EventLog
using System; using System.Diagnostics; using System.ServiceProcess; namespace WindowsService { static class Program { static void Main() { try { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } catch (Exception ex) { EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error); } } } }
然后,卸载旧服务,并通过这些修改重新部署该服务。启动服务并签出事件查看器/应用程序日志。您将看到真正的问题是什么,这是超时的根本原因。