title: Windows service with FileWatcher not working as expected
body: <p>A windows service (viz <strong>Email_Tool</strong>), which was working fine earlier, has not been working  properly lately.</p>
  
  <p><strong>Service Overview</strong></p>
  
  <ol>
  <li>Email_Tool is a windows service which polls a directory using FileWatchers. </li>
  <li>It keeps watching the directory and upon receiving certain files it sends out emails to the users.</li>
  <li>The path it watches is a shared path with different permissions for different users. My service uses a service account with Read permissions.</li>
  <li>The receipt of files and sending of emails get logged to a file (viz <strong>ServiceLog.txt</strong>).</li>
  <li>An example to explain the workflow:
  The folder with the name 21052014 (a folder with the name as current date is created) is created in the location mentioned above. In that folder few files arrive(Date.txt, InputsLog_20140520.txt, PTool_Start_20-05-2014.txt, PTool_End_20-05-2014.txt) . The service thus sends a mail, logging every step in a log file for support operations.</li>
  </ol>
  
  <p><strong>Problem faced</strong></p>
  
  <p>The Email_Tool has not been working as expected since few days. The steps are not getting logged in the log file and the emails are not being sent out.</p>
  
  <p><strong>Steps Taken</strong></p>
  
  <p>We have taken many steps to identify the problem. The following are few of them:</p>
  
  <ol>
  <li>To prove that the code is working, we used the local file path(F drive) of lower environment as the file path. The service worked fine and the mail was sent.</li>
  <li>We tried using the shared path on a lower environment as the path. The service worked fine and mail was sent.</li>
  <li>We tried using the shared path which is used in Production from other lower environments , where we created new folders and gave them appropriate permissions. This didn’t work out.</li>
  </ol>
  
  <p>As is clear, the service, which was working earlier,  is not working for the shared paths which were used in Production. </p>
  
  <p><strong>Information worth Noting</strong></p>
  
  <ol>
  <li>When we <strong>STOP</strong> and then <strong>START</strong> the service, and try to see the workflow by creating folder and moving in files(as mentioned in the service overview above), the service works for a couple of steps and then goes haywire. I can confirm this as I can see the steps for folder creation in the log file sometimes(not always). I am unable to understand why this works for a couple of steps and then stops to work as on other file servers it works perfectly.</li>
  <li>Just a day before the service went haywire, the shared path was moved to a different hadrware. We are almost sure that the problem has been caused by this. But the teams maintaining this shared path have confirmed that the permissions have been kept intact after the hardware migrations.</li>
  <li>I tried to debug the code by moving the code into a forms application and came up with the following numbers/codes:</li>
  </ol>
  
  <p><strong>BaseException</strong></p>
  
  <p>Base exception area gave the following codes:</p>
  
  <p>ErrorCode: -2147467259
  Native Error code : 58</p>
  
  <p>The above native error code 58 points to the following(google search)</p>
  
  <p>ERROR_BAD_NET_RESP
  58 (0x3A)
  The specified server cannot perform the requested operation.</p>
  
  <p><strong>InnerException</strong></p>
  
  <p>The inner exception showed the following code:</p>
  
  <p>_COMPlusExceptionCode            -532459699         int</p>
  
  <p><strong>Codes</strong></p>
  
  <p>the service <strong>start</strong> event has this code snippet</p>
  
  <pre><code>   m_Watcher = new System.IO.FileSystemWatcher();
              m_Watcher.Filter = "*.*";
              m_Watcher.Path = @filWatcherPath;
              m_Watcher.IncludeSubdirectories = true;
  
  
  
              m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
              m_Watcher.Created += new FileSystemEventHandler(OnChanged);
  
              //  m_Watcher.Error += new ErrorEventHandler(OnError);
  
              m_Watcher.EnableRaisingEvents = true;
  </code></pre>
  
  <p>As a workaround, we have used another file server temprarily which on which the service is working fine. But we have to find a fix. Please let me know if there is any other information that I must provide. I had posted this question earlier, but I think this post provides more details.</p>