简体   繁体   中英

Windows Service and database connection

(I am new to Windows Services) I have an automatic windows service that fetches some records from a database table, calls a web service method for every record and inserts the return value of the method and some other information into the table. My problems are listed below:

1- When I try to start the service manually I get Error 1053 (the service did not respond to the start or control request in a timely fashion) . By the way despite the error service starts and does what it has to do.

2- (I know it is a common problem but I prefer to ask it again) The service is Automatic but depends on the database connection. When the system the service is running on, is being restarted I had to start it manually. I add a Thread.Sleep() line of code to avoid this problem. What is the genuine way to solve the problem?

protected override void OnStart(string[] args)
    {
        checkRegistryForOracleNLS();

        //Creating event log
        plog = new System.Diagnostics.EventLog();
        if (!System.Diagnostics.EventLog.SourceExists("Prov"))
        {
            System.Diagnostics.EventLog.CreateEventSource("Prov", "ProvLog");
        }
        plog.Source = "Prov";
        plog.Log = "ProvLog";
        plog.Clear();
        Log("Service started at " + DateTime.Now.ToLongTimeString(), System.Diagnostics.EventLogEntryType.Information);

        class_ora_tran.int_class_ora_tran();
        DataTable dt;

        Thread.Sleep(300000); 

        while (true)
        {
            Con2DB();
            if (class_ora_tran.dbConnected())
            {
                RemoveOldRecords();
                dt = FetchRec();
                if (dt != null)
                {
                    if (dt.Rows.Count > BLKSize)
                        Bulk(dt);
                    else
                        Single(dt);
                }
            }
        }   
    }

3- Although I wrote the OnStop() method, there is no Stop function when the service is running and I want to stop it. I should also mention that my service is always in the Starting mode and never changes to Started.

Protected void OnStop(string[] args)
    {
        class_ora_tran.Disconnect();
        Log("Service stoped at " + DateTime.Now.ToLongTimeString(), System.Diagnostics.EventLogEntryType.Information);
        plog.Close();
    }

4- Every now and then, although the status is starting, my service stops doing what it has to do and does not fetch records from the table. It happened two times before, unfortunately I forgot to check Event Viewer to see if some errors happened. anybody know what is the cause? Is it related to the problems I mentioned above?

5- Every time I want to uninstall the service I had to run the uninstall command for two times! Any help?

Edit: Records are inserted into the table contentiously and the process of calling the web service and etc should be done whenever a new record has been entered to the table. I thought that a While(true) loop is needed. Any suggestion to replace this part?

I will put forth some suggestions for your service.

  1. Make your service to be a manual start. So once you install the service, you just start it manually just once.
  2. After you start, you have some good options to run your logic.

    . Run every X minutes http://www.codeproject.com/Questions/540617/windowsplusserviceplustoplusrunpluseveryplusoneplu

    . Run at hh:mm everyday Windows Service to run a function at specified time . Run immediately

  3. Put your code in a function and call this function in the Timer_Elapsed() function which ever you choose in the above link examples

  4. Use OnStart to setup the timer information, Reading Config files etc.

  5. Try using the windows Add/Remove programs to remove the service. (create an installer msi to install your service. Saves your command lines) How to create an installer for a .net Windows Service using Visual Studio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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