简体   繁体   English

C#Windows服务:错误1053

[英]c# windows service: error 1053

I'm new to Windows services and looked for a lot of examples and stuff, also searched questions asked before, but I did not yet find a solution for my problem. 我是Windows服务的新手,正在查找许多示例和资料,还搜索了之前提出的问题,但是我还没有找到解决问题的方法。 I'm writing a Windows service in c# that is sending SOAP requests to a server, organises the received data and makes it ready for storing it into the historian. 我正在用C#编写Windows服务,该服务将SOAP请求发送到服务器,对接收到的数据进行整理,并准备将其存储到历史数据库中。

Initially I made it a console application, but it needs to run scheduled in the background. 最初,我将其制作为控制台应用程序,但需要在后台按计划运行。 That's why I made the choice for a Windows service. 这就是为什么我选择Windows服务的原因。 As a console application the program takes at least 20 minutes, but this can be over an hour, depending on the amount of data. 作为控制台应用程序,该程序至少需要20分钟,但这可能要花一个多小时,具体取决于数据量。

The Windows service returns an error after like 30 seconds with code 1053: The service did not respond to the start or control request in a timely fashion. Windows服务在30秒后返回错误,代码为1053:Windows服务未及时响应启动或控制请求。 I think this has something to do with the service trying to run the whole code in onStart() and the service is thus not returning within a timely fashion. 我认为这与尝试在onStart()中运行整个代码的服务有关,因此该服务未及时返回。

I'm using the following design: 我正在使用以下设计:

myProgram.cs: 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.
}

In the Program.cs: 在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);
        }

    }
}

I hope you can help me with my problem. 我希望你能帮助我解决我的问题。

Thanks in advance! 提前致谢!

Koert 科特

检查事件查看器以查看服务引发的异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM