簡體   English   中英

Windows服務錯誤1053

[英]Windows Service Error 1053

我當前正在編寫一個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);

由於添加了所有內容,現在一切似乎都可以正常運行,該服務將按預期啟動。

似乎您的OnStart()方法未在允許的超時時間內返回returnig。 該消息是否立即出現,還是需要大約30秒鍾才能顯示? 您的CRM連接資料(tm)是否需要一段時間才能運行?

遠射:您是否在應用程序中使用任何Windows.Forms內容? 這些不應在服務上使用,並且可能以奇怪和神秘的方式進行交互。

我遇到了同樣的問題,根本不確定如何解決。 是的,發生這種情況是因為該服務引發了異常,但是可以遵循一些通用准則來更正此問題:

  • 檢查您是否編寫了正確的代碼來啟動服務:

    ServiceBase [] ServicesToRun; ServicesToRun = new ServiceBase [] {new WinsowsServiceToRun()}; ServiceBase.Run(ServicesToRun);

  • 您需要確保WinsowsServiceToRun類中正在運行某種無限循環

  • 最后,可能有些代碼沒有記錄任何內容並突然關閉程序(我就是這種情況),在這種情況下,您將必須遵循舊的調試方法,即需要在源代碼中寫一行代碼(文本/分貝/哪里)。 我所面對的是,由於運行該服務的帳戶不是“管理員”,因此即使正在嘗試記錄“ Windows事件日志”,該代碼只是掉下來並且沒有記錄任何異常,即使該代碼用於記錄異常。 登錄到偶數日志實際上並不需要管理員特權,但是定義源需要它。 如果事件的來源尚未在系統中定義,並且服務嘗試在沒有管理員特權的情況下首次記錄該事件,則它將失敗。 要解決此問題,請執行以下步驟:

  1. 以管理員權限打開命令提示符
  2. 粘貼命令:eventcreate / ID 1 / L應用程序/ T信息/ SO <> / D“ <>”
  3. 按回車
  4. 現在啟動服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM