我遇到了 Windows服务应用程序的问题。 该应用程序有一个计时器,每x秒执行一次功能。
通过在本地开发人员PC上测试应用程序,该服务可以正常运行。
通过在发布模式下对 Windows Server 2008 编译服务进行测试,当我启动该服务时,我收到以下错误
错误1053:服务没有及时响应启动或控制请求
如果我从服务器事件查看器进行检查,我会收到此信息
setup instructions from the doc
下面我留下我的一小段代码
private const int TICK_TIMER = 120000; //Start timer every 2 minutes
private Timer readTimer = null;
public BarcodeReader()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
readTimer = new Timer();
readTimer.Interval = TICK_TIMER;
readTimer.Elapsed += readTimer_Tick;
readTimer.Enabled = true;
WriteLog("Servizio Barcode started");
}
private void readTimer_Tick(object sender, ElapsedEventArgs e)
{
//Start function
try
{
MyFunction();
}
catch (Exception ex)
{
WriteLog("ERROR: " + ex.Message);
}
}
private void WriteLog(string mex)
{
try
{
//sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
using (var sw = new StreamWriter(Globals.LogPath + "LogBarcodeService.txt", true))
{
sw.WriteLine(DateTime.Now.ToString(CultureInfo.CurrentCulture) + ": " + mex);
sw.Flush();
sw.Close();
}
}
catch (Exception)
{
throw;
}
}
protected override void OnStop()
{
readTimer.Enabled = false;
WriteLog("Servizio Barcode terminated");
}
N.B。在服务器上.NET Framework 4.5安装在Developer PC上
这是对InitializeComponent
功能
namespace BarcodeReaderService
{
partial class BarcodeReader
{
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
/// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Codice generato da Progettazione componenti
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
#endregion
}
}
我尝试将所有代码放入应用程序控制台并在服务器上顺利运行。
答案 0 :(得分:1)
解决方案应该包括输出目录中的所有内容(例如bin \ Debug)以复制到服务器上的某个文件夹。从那里运行InstallUtil来注册服务。
另一种方法是为Windows服务创建安装程序。
答案 1 :(得分:1)
要检查的一件事是开发机器和服务器之间的.Net框架版本。
当我们的开发机器有.Net 4.7(截至目前为止的最新版本)且服务器有4.5时,我们遇到了类似的问题。
在我们的案例中,我们还在我们的事件查看器中登录了以下内容
第一个例外
框架版本:v4.0.30319
描述:由于未处理的异常,该进程已终止。
异常信息:System.IO.FileLoadException
第二个例外条目
错误模块名称:KERNELBASE.dll,版本:6.3.9600.18340,时间戳:0x5736541b
异常代码:0xe0434352
故障偏移:0x00014878
您可以使用本指南查找确切版本: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
请注意,更高版本是就地升级!所以,一旦你拥有最新版本,就无法回到旧版本了!
https://docs.microsoft.com/en-us/dotnet/framework/install/guide-for-developers
以后的.NET Framework 4.x版本对早期版本进行就地更新这一事实意味着如果已安装更高版本,则无法安装表中列出的早期版本