[英]C# Error 1053 during start up Windows Service
我對Windows服務應用程序有疑問。 該應用程序具有一個計時器,該計時器每x秒執行一次功能。
通過在本地開發人員PC上測試應用程序,該服務可以正常運行。
通過在Windows Server 2008上也以發布模式編譯測試該服務,當我啟動該服務時,出現以下錯誤
錯誤1053:服務未及時響應啟動或控制請求
如果我從服務器事件查看器中進行檢查,則會得到此信息
下面我留下了我的小代碼段
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");
}
注意:在服務器上,NET Framework 4.5的安裝與開發人員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
}
}
我試圖將所有代碼帶入應用程序控制台,並在服務器上平穩運行它們。
解決方案應該包括將輸出目錄中的所有內容(例如bin \\ Debug)復制到服務器上的某個文件夾中。 從那里運行InstallUtil來注冊服務。
另一種方法是為Windows服務創建安裝程序。
要檢查的一件事是開發計算機和服務器之間的.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/zh-cn/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更高版本是對早期版本的就地更新這一事實意味着,如果已經安裝了更高版本,則無法安裝表中列出的早期版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.