簡體   English   中英

C#Windows服務:錯誤1053

[英]c# windows service: error 1053

我是Windows服務的新手,正在查找許多示例和資料,還搜索了之前提出的問題,但是我還沒有找到解決問題的方法。 我正在用C#編寫Windows服務,該服務將SOAP請求發送到服務器,對接收到的數據進行整理,並准備將其存儲到歷史數據庫中。

最初,我將其制作為控制台應用程序,但需要在后台按計划運行。 這就是為什么我選擇Windows服務的原因。 作為控制台應用程序,該程序至少需要20分鍾,但這可能要花一個多小時,具體取決於數據量。

Windows服務在30秒后返回錯誤,代碼為1053:Windows服務未及時響應啟動或控制請求。 我認為這與嘗試在onStart()中運行整個代碼的服務有關,因此該服務未及時返回。

我正在使用以下設計:

myProgram.cs:

public MyService()
{
        InitializeComponent();
        ExecuteProgram();
}
protected override void OnStart(string[] args)
{
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000 * 60 * 60 *24; // every 24h  
    timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
    timer.Start();
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
    ExecuteProgram();
}
protected override void OnStop()
{
}
public void ExecuteProgram()
{
     //Here is the code for SOAP requests, preparing data and for making the 
       import file for the historian.
}

在Program.cs中:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        try
        {
#if (DEBUG)
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new MyService()
            };
            ServiceBase.Run(ServicesToRun);
#else
            if (Environment.UserInteractive)
            {
                string parameter = string.Concat(args);
                switch (parameter)
                {
                    case "--install":
                        ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                        break;
                    case "--uninstall":
                        ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                        break;
                }
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new MyService() 
                };
                ServiceBase.Run(ServicesToRun);
            }
#endif
        }
        catch (Exception ex)
        {
            throw (ex);
        }

    }
}

我希望你能幫助我解決我的問題。

提前致謝!

科特

檢查事件查看器以查看服務引發的異常

暫無
暫無

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

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