2

I tried to create a Windows Service with Oracle Db Access, but when I run the service it's not running the DB access, only printing the services started message on assigned text file.

But I assigned this same code to C# console application, everything works fine even the DB access part, what am I missing?

This is my code:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        WriteToFile("Service is Started at" + DateTime.Now);

        string finalName = string.Empty;
        DataSet ds;
        try
        {
            DataAccess dataAccess = new DataAccess();
            OracleParameter[] paramArray = new OracleParameter[1];
            paramArray[0] = new OracleParameter("P_ResultSet", OracleDbType.RefCursor);
            paramArray[0].Direction = ParameterDirection.Output;

            ds = dataAccess.ReadDataBySP(paramArray, "SP_GET_ALL_SCREEN");

            if (ds != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    finalName += row["DISPLAYNAME"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        WriteToFile(finalName);
    }
    protected override void OnStop()
    {
        WriteToFile("Service is Stopped at" +DateTime.Now);
    }

    public static void WriteToFile(string Message)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
        if (!File.Exists(filepath))
        {
            // creating file
            using (StreamWriter sw = File.CreateText(filepath))
            {
                sw.WriteLine(Message);
            }
        }
        else
        {
            using(StreamWriter sw = File.AppendText(filepath))
            {
                sw.WriteLine(Message);
            }
        }
    }
}

please Note that WriteToFile () method works fine.

  • 1
    Be careful doing something lengthy in `OnStart`. You might have to tell SCM about startup state. Generally service logic runs in a child thread – MickyD Oct 30 '18 at 04:39
  • No exceptions thrown (= Windows Service still running)? Have also a look at the EventLog of the server running this service and check your database configuration. – Jeroen Heier Oct 30 '18 at 04:48
  • @MickyD Thanks for the tip, I'll keep mind that, yes i assigned the startup state to start also, – Janaka Nanayakkara Oct 30 '18 at 05:06
  • 1
    I can tell you why app can run and services don't. Because services have limited access to computer resources. At least, you can setup some admin user and try to run service as that user. And whatever you do there on start, you should do it on background thread. Your service should start without doing what you doing there. Are you using oracle managed? – T.S. Oct 30 '18 at 05:19
  • @JeroenHeier thanks for the comment, I checked the eventlog and database configuration, db config is works fine, but eventlog is not showing any record about this service. – Janaka Nanayakkara Oct 30 '18 at 05:20
  • @T.S. Thank you very much for your advice. I tried to run with a background thread now, but still showing no output from Database, but printing the process start message on the assigned text file – Janaka Nanayakkara Oct 30 '18 at 05:38
  • Have a look at [this](https://stackoverflow.com/questions/1196531/how-to-debug-the-net-windows-service-onstart-method) SO question to debug your code. – Jeroen Heier Oct 30 '18 at 16:48
  • @JeroenHeier I thanks again for the comment, i found the issue of my code, issue was It's not having the access to other classes in the project, my DB access on other class. So It was fixed when i wrote that DB class's codes on same class, Now Service is retrieving data, and works fine Thank you all!! – Janaka Nanayakkara Oct 31 '18 at 04:36

0 Answers0