[英]Error 1053: the service did not respond to the start or control request in a timely fashion
[英]Error 1053 the service did not respond to the start or control request in a timely fashion
我已经创建并安装了几次服务。 最初它工作正常,但是在服务代码中进行了一些更改后,当我在 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);
}
我还尝试了以下步骤:
我曾经使用以下命令安装和卸载它:
installutil AutoSMS.exe
installutil /u AutoSMS.exe
就我而言,我在调试模式下发布服务。
解决方案是:
InstallUtil -u WindowsServiceName.exe
卸载旧服务InstallUtil -i WindowsServiceName.exe
之后效果很好。
正如其他人指出的那样,此错误可能有多种原因。 但希望这能帮助别人,我将分享我们案例中发生的事情。 对我们来说,我们的服务已经升级到 .NET 4.5,但是服务器没有安装 .NET 4.5。
在这个问题上花了一些时间,尝试了不起作用的解决方案后,我遇到了这个博客。 它建议将服务初始化代码包装在 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);
}
}
}
}
然后,卸载旧服务,使用这些修改重新部署服务。 启动服务并检查事件查看器/应用程序日志。 您将看到真正的问题是什么,这就是超时的根本原因。
我遇到了同样的问题,完全不知道如何解决。 是的,这是因为服务引发了异常,但您可以遵循一些一般准则来纠正此问题:
ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WinsowsServiceToRun() }; ServiceBase.Run(ServicesToRun);
ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WinsowsServiceToRun() }; ServiceBase.Run(ServicesToRun);
您需要确保在 WinsowsServiceToRun 类中运行某种无限循环
最后,可能有一些代码没有记录任何内容并突然关闭程序(我就是这种情况),在这种情况下,您将不得不遵循需要向源代码写入一行的老式调试方法(文本/db/任何地方)。 我面临的是,由于运行该服务的帐户不是“管理员”,因此代码只是脱落并且没有记录任何异常,以防它试图写入“Windows事件日志”,即使代码在那里记录异常. 登录 Even Log 实际上不需要管理员权限,但需要定义源。 如果系统中尚未定义事件源并且服务尝试在没有管理员权限的情况下首次记录它,则会失败。 要解决此问题,请执行以下步骤:
eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO <<Source>> /D "<<SourceUsingWhichToWrite>>"
我刚刚在 .Net 4.5 中本地尝试了此代码,并且该服务为我正确启动和停止。 我怀疑您的问题可能与创建 EventLog 源有关。
方法:
EventLog.SourceExists("MySource")
要求运行代码的用户必须是管理员,根据此处的文档:
http://msdn.microsoft.com/en-us/library/x7y6sy21(v=vs.110).aspx
检查该服务是否以具有管理员权限的用户身份运行。
这是因为 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
或者确保在那一刻没有另一个进程与服务交谈,也许有冲突我不知道
我遇到了完全相同的问题,我所做的就是在编译 dll 时将调试模式更改为发布。 这解决了我的问题,如何/为什么? 我不知道我已经问过一个关于 SO 的问题
在这个问题上花费了太多时间之后。 尽管我使用得当,但我发现EventLog
导致了所有的混乱。
无论谁解决此问题,我都建议您摆脱EventLog
。 使用更好的工具,例如“ log4net ”。
此外,您需要检查您的配置文件内容。
您需要检查以下部分。
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
因为上面的部分需要与你的 .net 框架相匹配。
在我今天早上遇到的情况下,罪魁祸首是一个格式错误的配置文件。 配置文件有一个关闭评论标签,没有打开评论标签。 因此,请仔细检查您的配置文件是否有错误。
如果您想将 .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();
}
}
}
现在您可以安装可执行文件并将其作为 Windows 服务启动:
sc.exe create ConsoleApp1 binPath= "<BIN_PATH>\ConsoleApp1.exe"
如果您使用EntityFramework
或任何其他方式在服务启动时启动数据库连接,请检查ConnectionStrings
。
就我而言,当我收到错误Error 1053 the service did not respond to the start or control request in a timely fashion
这就是问题所在:
我正在使用 EntityFramework 并且连接字符串错误。 所以基本上在启动时,EntityFramework 无法使用不正确的连接字符串连接到数据库并超时。
只需用正确的连接字符串替换数据库连接字符串,它就可以正常工作。
根本不需要任何框架更新或任何其他系统/配置更改。
我已经在我的 WindowsServer 计算机上安装了 .Net 4.6,并且错误已修复。
你的控制应该是这样的:
这个问题的一种可能的解决方案[它在我的最后解决了问题,我的应用程序是基于JAVA的应用程序]:
1) 检查您的应用程序是否指向正确的 java 版本(检查应用程序中的 java 版本和路径)。
或者
2)检查配置的java版本,即检查它是32位版本还是64位版本(根据您的应用程序)。 如果您使用的是 32 位,那么您应该使用 32 位版本的 JSL,否则 JSL 会导致此问题。
这通常是由服务本身中未捕获的异常引起的。 (例如配置文件错误)。 打开命令提示符并手动启动服务可执行文件可能会显示引发的异常。
根据我的经验,我不得不停止现有服务来更新代码。 更新代码并启动服务后,我得到了同样的错误“错误 1053,服务没有及时响应启动或控制请求”。
但这在重新启动机器后得到了解决。
我知道这是个老问题,我曾经编写自己的 VB.NET windows 服务,在 MS windows 7 和 MS windows 10 上启动没有问题。
当我在最新的 MS Windows 10 补丁上安装 Windows 服务时,我遇到了这个问题。 windows 服务不运行的原因是,windows 服务运行所需的 .NET 版本没有出现在已安装的 PC 中。
安装完windows服务后。 转到安装文件夹,例如 C:\Program files (x86)\Service1\Service1.exe 并双击运行它。 如果缺少 .NET 框架包,它会提示用户下载它。 只需下载并等待它安装。
之后重新启动 services.msc 中的 Windows 服务。 希望这个答案能帮助面临这个问题的人。 我知道问题是由 .NET 框架版本引起的。
我挠了挠头来清除这个错误如果你在代码中调试它可能会导致这个错误
static void Main()
{
#if DEBUG
MailService service = new MailService();
service.Ondebug();
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MailService()
};
ServiceBase.Run(ServicesToRun);
#endif
}
}
像这样清除代码中的if,else
和endif
后,错误不再出现....希望对您有所帮助....
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MailService()
};
ServiceBase.Run(ServicesToRun);
}
我想为此添加我的解决方案。 必须承认,我使用了一个额外的配置文件(“ServiceIni.xml”),其中一些设置可以由用户即时更改。 当我遇到此错误时,我进行了研究并执行了以下操作:
static void Main()
{
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
}
catch (Exception ex)
{
LogChanges($"Error - {ex.Message}\nInner - {ex.InnerException}");
}
}
static void LogChanges(string message)
{
string LogPath = AppDomain.CurrentDomain.BaseDirectory + "MyServiceLog.txt";
using (StreamWriter wr = File.AppendText(LogPath))
{
wr.WriteLine(message);
}
}
Error - Configuration system failed to initialize
Inner - System.Configuration.ConfigurationErrorsException: A section using 'configSource' may contain no other attributes or elements. (C:\...\bin\Release\MyService.exe.Config line 24)
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
<!-- Setting to use appSettings from external file -->
<appSettings configSource="ServiceIni.xml"/>
至
<appSettings configSource="ServiceIni.xml">
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
这产生了这个错误。
修复回原来的 App.config 解决了这个问题。
就我而言,问题在于 App.config 文件中设置的缓存配置。 一旦我从 App.config 文件中删除以下行,问题就解决了。
<cachingConfiguration defaultCacheManager="MyCacheManager">
<cacheManagers>
<add name="MyCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
expirationPollFrequencyInSeconds="60"
maximumElementsInCacheBeforeScavenging="50000"
numberToRemoveWhenScavenging="1000"
backingStoreName="NullBackingStore" />
</cacheManagers>
<backingStores>
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="NullBackingStore" />
</backingStores>
我已删除
EventLog.Exists
并固定。
就我而言,我的 App.config 文件中显然有一些额外的符号,我在构建解决方案时没有注意到这些符号。 因此,我建议在更改注册表项、切换配置模式等步骤之前先检查配置文件是否有错误。
我有两个正在运行的服务意外耦合到同一个 EventSource eventLog.Source = "MySource";
卸载这两项服务并重新安装出现错误 1053 的服务后,我的服务正常启动。
检查服务启动码是否正确,
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WinsowsServiceToRun()
};
ServiceBase.Run(ServicesToRun);
此外,删除所有调试代码。 IE,
#If Debug
...
...
...
#else
...
...
#endif
我有同样的问题。 取消选中项目属性中的“Sing the ClickOnce manifest”、“签署程序集”和“启用 ClickOnce 安全设置”有助于
我的问题是appsettings.json
在构建过程中没有复制到发布构建文件夹并且简单地将其放入...\bin\Release
文件夹并将launchSettings.json
内容复制到appsettings.json
解决了我的问题。
我遇到过同样的问题。 似乎在启动服务时主线程不应该是主工作线程。 通过简单地创建一个新线程并将主要工作交给该线程解决了我的问题。
由于各种原因,可能会导致此错误。 确定在服务运行时添加 try/catch 的原因。
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
<span class="skimlinks-unlinked">ServiceBase.Run(ServicesToRun</span>);
}
catch (Exception ex)
{
EventLog.WriteEntry("Application", ex.ToString(), <span class="skimlinks-unlinked">EventLogEntryType.Error</span>);
}
安装 .net 框架 4.5! 它对我有用。
https://www.microsoft.com/en-us/download/details.aspx?id=57768
如果您有 .NET 6,请阅读它
https://docs.microsoft.com/en-us/dotnet/core/extensions/windows-service
在命令行中创建服务时,像这样使用
sc.exe create SERVICE_NAME binPath="C:\....\bin\Release\net6.0\win-x64\SERVICE_NAME.exe"
一个可能的原因是:
Windows服务.net版本和您的系统.net版本不匹配。
建立服务
要在解决方案资源管理器中构建服务项目,请打开项目的上下文菜单,然后选择“属性”。 将显示项目的属性页。 在“应用程序”选项卡上的“启动对象”列表中,选择“MyService.Program”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.