我目前正在编写一个 Windows 服务,它连接到 crm 系统以拉下一个计划,然后运行各种数据源等。我已经一切正常,除了当我安装所有内容并尝试运行启动服务时,我收到以下错误:
“错误 1053:服务未响应启动或控制请求
及时时尚”
这是我在 Service1.cs 中使用的代码;
namespace FeedManagementService
{
public partial class Service1 : ServiceBase
{
private System.Timers.Timer timer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// Instantiate the timer
Thread t = new Thread(new ThreadStart(this.InitTimer));
t.IsBackground = true;
t.Start();
} // OnStart
protected override void OnStop()
{
timer.Enabled = false;
} // OnStop
private void InitTimer()
{
timer = new System.Timers.Timer();
// Add the timer event
timer.Elapsed += new ElapsedEventHandler(timerTick);
// Set the interval
double timeInSeconds = 6.0;
timer.Interval = (timeInSeconds * 1000);
timer.Enabled = true;
} // InitTimer()
private void timerTick(object sender, EventArgs e)
{
// CRM connection stuffhere
} // timerTick
}
}
然后在Service1.Designer.cs中添加以下内容
namespace FeedManagementService
{
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()
{
this.components = new System.ComponentModel.Container();
this.ServiceName = "Feed Management Service";
this.CanPauseAndContinue = true;
} // InitializeComponent()
#endregion
}
}
最后是 ProjectInstaller.cs 中的以下内容
namespace FeedManagementService
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
ServiceProcessInstaller process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
ServiceInstaller serviceAdmin = new ServiceInstaller();
serviceAdmin.StartType = ServiceStartMode.Manual;
serviceAdmin.ServiceName = "Service1";
serviceAdmin.DisplayName = "Feed Management Service";
Installers.Add(process);
Installers.Add(serviceAdmin);
}
}
}
经过大量调查并修复了一大堆与该问题无关的“问题”后,我发现我需要在服务的 Main() 方法中执行以下操作;
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
添加此内容后,现在一切似乎都工作正常,服务按预期启动。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)