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.