Ale*_*lex 36 c# windows error-handling windows-services
我在C#中编写了一个Windows服务,它基本上每分钟检查我的数据库的订单,从这些订单生成PDF,并通过电子邮件发送.
逻辑在我的测试等中完美运行.
当我创建服务并使用安装项目安装它时,当我在服务mmc中启动服务时,我得到:
错误1053服务未及时响应启动或控制请求
我的OnStart方法如下所示:
protected override void OnStart(string[] args)
{
//writeToWindowsEventLog("Service started", EventLogEntryType.Information);
timer.Enabled = true;
}
Run Code Online (Sandbox Code Playgroud)
基本上,只需启用计时器......所以没有进程密集的呼叫.
我哪里错了?
我已经尝试将启动帐户设置为本地系统,网络服务等......没有任何作用!
编辑:
这是我的代码:(processPurchaseOrders是查询数据库并生成pdf的方法等...)
public partial class PurchaseOrderDispatcher : ServiceBase
{
//this is the main timer of the service
private System.Timers.Timer timer;
public PurchaseOrderDispatcher()
{
InitializeComponent();
}
// The main entry point for the process
static void Main()
{
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new PurchaseOrderDispatcher() };
ServiceBase.Run(ServicesToRun);
#else //debug code
PurchaseOrderDispatcher service = new PurchaseOrderDispatcher();
service.processPurchaseOrders();
#endif
}
private void InitializeComponent()
{
this.CanPauseAndContinue = true;
this.ServiceName = "Crocus_PurchaseOrderGenerator";
}
private void InitTimer()
{
timer = new System.Timers.Timer();
//wire up the timer event
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
//set timer interval
var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]);
timer.Interval = (timeInSeconds * 1000); // timer.Interval is in milliseconds, so times above by 1000
timer.Enabled = true;
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
}
protected override void OnStart(string[] args)
{
//instantiate timer
Thread t = new Thread(new ThreadStart(this.InitTimer));
t.Start();
}
protected override void OnStop()
{
//turn off the timer.
timer.Enabled = false;
}
protected override void OnPause()
{
timer.Enabled = false;
base.OnPause();
}
protected override void OnContinue()
{
timer.Enabled = true;
base.OnContinue();
}
protected void timer_Elapsed(object sender, ElapsedEventArgs e)
{
processPurchaseOrders();
}
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*sCa 29
我刚遇到同样的问题.
事实证明这是因为我在调试模式下将其作为控制台运行 - 就像上面的代码一样
#if (!DEBUG)
#else //debug code
#endif
Run Code Online (Sandbox Code Playgroud)
我在调试模式下编译并安装了服务
当我在发布模式下编译它时,它按预期工作
希望这可以帮助
SwD*_*n81 27
来自MSDN:
"不要使用构造函数来执行应该在OnStart中的处理.使用OnStart来处理服务的所有初始化.构造函数在应用程序的可执行文件运行时调用,而不是在服务运行时调用.可执行文件在OnStart之前运行.例如,当你继续时,不会再次调用构造函数,因为SCM已经将对象保存在内存中.如果OnStop释放在构造函数而不是OnStart中分配的资源,则第二次服务时不会再次创建所需的资源.被称为".
如果您的计时器未在OnStart调用中初始化,则可能是一个问题.我还会检查计时器的类型,确保它是一个System.Timers.Timer for Services.以下是如何在Windows服务中设置计时器的示例.
// TODONT:使用Windows服务只是为了运行计划的进程
我尝试了你的代码,似乎没问题.我唯一的区别是硬编码定时器值(Service1.cs).如果以下内容不起作用,请告诉我.
Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Threading;
namespace WindowsServiceTest
{
public partial class Service1 : ServiceBase
{
private System.Timers.Timer timer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//instantiate timer
Thread t = new Thread(new ThreadStart(this.InitTimer));
t.Start();
}
protected override void OnStop()
{
timer.Enabled = false;
}
private void InitTimer()
{
timer = new System.Timers.Timer();
//wire up the timer event
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
//set timer interval
//var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]);
double timeInSeconds = 3.0;
timer.Interval = (timeInSeconds * 1000);
// timer.Interval is in milliseconds, so times above by 1000
timer.Enabled = true;
}
protected void timer_Elapsed(object sender, ElapsedEventArgs e)
{
int timer_fired = 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Service1.Designer.cs
namespace WindowsServiceTest
{
partial class Service1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
this.CanPauseAndContinue = true;
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
我刚刚创建了一个空白的Windows服务项目并添加以下内容,因此我可以运行installutil.exe并附加到上面以查看事件是否正在触发(并且确实如此).
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;
namespace WindowsServiceTest
{
[RunInstaller(true)]
public class MyServiceInstaller : System.Configuration.Install.Installer
{
public MyServiceInstaller()
{
ServiceProcessInstaller process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
ServiceInstaller serviceAdmin = new ServiceInstaller();
serviceAdmin.StartType = ServiceStartMode.Manual;
serviceAdmin.ServiceName = "Service1";
serviceAdmin.DisplayName = "Service1 Display Name";
Installers.Add(process);
Installers.Add(serviceAdmin);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Aar*_*ron 11
如果将来有其他人遇到这种情况,我在Windows Server 2012上尝试启动Windows服务时收到了相同的错误1053.
问题最终是该服务是针对.NET framework 4.5.1开发的,但Windows Server 2012实例没有安装该.NET版本的.NET框架.将服务备份到目标.NET 4.0修复了错误.
当我看到这样的问题时,我总是先去查看事件查看器。它确实为初步调查和调试提供了更多重要的细节。
很多时候,我们会忘记事件查看器会捕获未处理的 .net 运行时异常,这些异常可以在Windows 日志>应用程序下找到,然后按事件 ID 1026(.NET 运行时)进行筛选