1

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.

8
  • 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 Commented Oct 30, 2018 at 4: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. Commented Oct 30, 2018 at 4:48
  • @MickyD Thanks for the tip, I'll keep mind that, yes i assigned the startup state to start also, Commented Oct 30, 2018 at 5: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? Commented Oct 30, 2018 at 5: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. Commented Oct 30, 2018 at 5:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.