gpt4 book ai didi

c# - 1053 windows 服务没有及时响应

转载 作者:可可西里 更新时间:2023-11-01 11:34:18 26 4
gpt4 key购买 nike

我有一个在 IIS 中运行的 Web 应用程序,并通过托管在同一台机器上的 WCF 与 Windows 服务通信。 Windows 服务在内部执行各种作业,有时它非常忙于处理用户请求。一开始我遇到了服务启动问题,因为它有时需要很长时间才能启动并最终抛出错误,即服务超时。为了解决这个问题,我将代码移到了另一个线程而不是主线程。一切正常,但现在我观察到有时我在服务停止时遇到错误,即

**1053 windows service did not respond in timely fashion**

我的服务 OnStop() 代码:

protected override void OnStop()
{
// Stop the WCF service
if (myServiceHost != null)
{
myServiceHost.Close();
myServiceHost = null;
}

// Stop the scheduler

if (myScheduleManager != null)
{
myScheduleManager.Stop();
}

// Update the registry value
Logger.GetInstance().LogEvent(this.GetType().Name, LogLevel.INFO, "Updating registry...");
Settings.GetInstance().LastStopTime = DateTime.Now.ToString();

Logger.GetInstance().LogEvent(this.GetType().Name, LogLevel.INFO, "Service stopped.");
}

一般发生在Service太忙的时候。但最终它在 30-40 分钟后停止。我知道这是由于 Windows 服务中的超时问题而发生的,因为默认时间非常短并且将其视为超时。

我的问题是什么是避免此类瓶颈的最佳做法意味着我是否应将与停止相关的任务移至另一个线程下并进行异步调用以释放该线程中的资源。但是,如果在释放资源期间,用户将启动服务,会发生什么情况?

最佳答案

只是猜测,但也许它有助于追查问题:

您有这个 myScheduleManager.Stop(),(我猜)它等待进程完成。这很好,但 Windows 只给你 30 秒来执行 ServiceBase.OnStop()

如果您在这里进行异步处理,您或许可以使用 .NET-Framework 中的 CancellationTokens 来实现 CancellationPattern。

关于c# - 1053 windows 服务没有及时响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com