1

Below is the code written for window services for copying files. It terminate from line "JsonConvert.DeserializeObject>(jsonStirng);"

public static void copyfiles()
{
    getLog("Before querying");
    SqlDataReader reader = select();
    if (reader.HasRows)
    {
        reader.Read();               
        string jsonStirng = reader["parameter"].ToString();
        getLog("Found rows" + jsonStirng); // this will get log with values
        try
        {

            var result = JsonConvert.DeserializeObject<List<RootObject>>(jsonStirng);
            getLog("result =>" + result[0].FOLDER_NAME); // this ignore, if remove above line, this log will work.
            // continue logic for copying files..
            }

        }
        catch (Exception e)
        {
            getLog("Fail to copy files"+e);
        }

    }
    else
    {
        getLog("No rows found.");
    }
    reader.Close();
}

Below is the log i am getting

ID  DATE    THREAD  LEVEL   LOGGER  MESSAGE
141 2019-05-22 17:09:02.273 Windows Services-Copy files INFO    OS  Found rows[{"FOLDER_TYPE":"UPLOAD_FOLDER","FOLDER_NAME":"UPLOAD_FOLDER\\"},{"FOLDER_TYPE":"DATA_FOLDER","FOLDER_NAME":"E:\\Suresh\\Projects\\TribandBISE\\BISE_C\\BISE_C\\DATA_FOLDER\\"},{"FOLDER_TYPE":"SOURCE_FOLDER","FOLDER_NAME":"SOURCE_FOLDER\\"},{"FOLDER_TYPE":"DB_BACKUP_FOLDER","FOLDER_NAME":"DATA_FOLDER\\DB_BACKUP_FOLDER"}]
140 2019-05-22 17:09:02.250 Windows Services-Copy files INFO    OS  inside select 
139 2019-05-22 17:09:02.240 Windows Services-Copy files INFO    OS  Before querying
138 2019-05-22 17:09:02.230 Windows Services-Copy files INFO    OS  Code initiated
137 2019-05-22 17:09:02.220 Windows Services-Copy files INFO    OS  windows service Copy_Data_Files initiated
136 2019-05-22 17:05:40.353 Windows Services-Copy files INFO    OS  error in init windows service
135 2019-05-22 17:05:40.050 Windows Services-Copy files INFO    OS  Code initiated
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
  • 1
    What exception do you get? Could you please add the exception details to your question (type, message, etc.)? – bassfader May 22 '19 at 11:56
  • it is not coming to catch block and even "Event Viewer" shows "Service started successfully." – Suresh Kamrushi May 22 '19 at 11:58
  • Can you show us what the log output looks like? You appear to be logging data at various points -- which log entries are getting hit? – bvoyelr May 22 '19 at 12:04
  • @bvoyelr added logs – Suresh Kamrushi May 22 '19 at 12:07
  • Thanks! I'm surprised that you're not getting something else in the logs. Can you provide the `RootObject` class? It should have a public, empty constructor. Also, you might try removing the `+e` from the exception log (or changing it to `e.Message`). It could be causing some other exception that is preventing that log from working (it SHOULDN'T be causing another exception, but it could be causing one). – bvoyelr May 22 '19 at 12:14
  • RootObject is correct only because, tested the code in web application it is working fine. Let me change the e to e.Message – Suresh Kamrushi May 22 '19 at 12:21
  • 1
    According to your log, it says "error in init windows service". Where is the code that logs that message? – Chris Dunaway May 22 '19 at 15:21
  • Hi Suresh. i am having exactly the same issue.. did you find the cause? – Ansenagy Jun 29 '19 at 17:31
  • @Ansenagy: Did not get solution to it. I change json to db then. – Suresh Kamrushi Jul 01 '19 at 06:16

1 Answers1

0

You can step through your service like a normal application in vs by altering the main entry point of the service.

static void Main()
{
/////IF DEBUG The service will create all resource and allow code stepthrough
#if (!DEBUG)
    //NO DEBUG - Production code ran here
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
    // Debug code: this allows the process to run as a non-service.
    // It will kick off the service start point, but never kill it.
    // Shut down the debugger to exit
    MyService service = new MyService();
    service.OnStart(null);    
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif 
}
Ross Bush
  • 14,648
  • 2
  • 32
  • 55