我已经创建并安装了几次服务.最初它工作正常,但在服务代码中的一些更改后,当我在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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的主要方法代码
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new AutoSMS()
};
ServiceBase.Run(ServicesToRun);
}
Run Code Online (Sandbox Code Playgroud)
我还尝试了以下步骤:
我曾经使用以下命令安装和卸载它:
installutil AutoSMS.exe
installutil /u AutoSMS.exe
Run Code Online (Sandbox Code Playgroud)
Pra*_*ngh 26
就我而言,我在调试模式下发布服务.
解决方案是:
InstallUtil -u WindowsServiceName.exeInstallUtil -i WindowsServiceName.exe它之后运作得很好.
Jam*_*phy 25
正如其他人所指出的那样,这个错误可能有很多原因.但是希望这会帮助别人,我将分享我们案件中发生的事情.对我们来说,我们的服务已升级到.NET 4.5,但服务器没有安装.NET 4.5.
小智 10
我遇到了同样的问题,并不确定如何解决它.是的,这是因为服务引发了异常,但您可以遵循一些通用准则来纠正此问题:
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WinsowsServiceToRun()
};
ServiceBase.Run(ServicesToRun);
您需要确保在WinsowsServiceToRun类中运行某种无限循环
最后,可能有一些代码没有记录任何内容并突然关闭程序(我就是这种情况),在这种情况下,你将不得不按照旧的调试学校来编写一行来源(文本/分贝/哪里).我面临的问题是,由于运行该服务的帐户不是"管理员",因此代码刚刚下降并且没有记录任何异常,以防它尝试写入"Windows事件日志",即使代码是用于记录异常.记录到偶数日志实际上不需要管理员权限,但需要定义源.如果事件的源尚未在系统中定义,并且服务尝试在没有管理员权限的情况下首次记录它,则它将失败.要解决此问题,请按照以下步骤
eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO <<Source>> /D "<<SourceUsingWhichToWrite>>"JS5*_*JS5 10
花了一些时间在问题上,尝试了无效的解决方案后,我遇到了这个博客。建议将服务初始化代码包装在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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,卸载旧服务,并通过这些修改重新部署该服务。启动服务并签出事件查看器/应用程序日志。您将看到真正的问题是什么,这是超时的根本原因。
我刚刚在.Net 4.5中尝试了本地代码,服务正常启动和停止.我怀疑你的问题可能在于创建EventLog源码.
方法:
EventLog.SourceExists("MySource")
Run Code Online (Sandbox Code Playgroud)
要求运行代码的用户必须是管理员,根据此处的文档:
http://msdn.microsoft.com/en-us/library/x7y6sy21(v=vs.110).aspx
检查该服务是否以具有管理员权限的用户身份运行.
小智 8
如果您想将 .NET core 3.1 可执行文件注册为 Windows 服务,请确保在 3.1.7 或更高版本中添加 nuget 包 Microsoft.Extension.Hosting.WindowsServices 并初始化 hostBuilder,如以下示例所示:
using Microsoft.Extensions.Hosting;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] arguments)
{
IHostBuilder hostBuilder = Host.CreateDefaultBuilder(arguments);
hostBuilder.UseWindowsService();
hostBuilder.Build().Run();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在您可以安装可执行文件并将其作为 Windows 服务启动:
sc.exe create ConsoleApp1 binPath= "<BIN_PATH>\ConsoleApp1.exe"
Run Code Online (Sandbox Code Playgroud)
这是因为 Microsoft Windows 服务控制,它有时会控制服务的状态。如果服务没有在 30 秒内发送响应,那么您将收到此错误。
可以修改注册表,让服务有更多时间响应
Go to Start > Run > and type regedit
Navigate to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
With the control folder selected, right click in the pane on the right and select new DWORD Value
Name the new DWORD: ServicesPipeTimeout
Right-click ServicesPipeTimeout, and then click Modify
Click Decimal, type '180000', and then click OK
Restart the computer
Run Code Online (Sandbox Code Playgroud)
或者确保在那一刻没有另一个进程与服务交谈,也许存在我不知道的冲突
在这个问题上花了太多时间之后。EventLog尽管使用得当,但我发现了所有混乱的原因。
无论谁解决此问题,我都建议您摆脱EventLog。使用更好的工具,例如“ log4net ”。