windows service not working for everyone.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayush patel
    New Member
    • Jul 2008
    • 63

    windows service not working for everyone.

    Hi I have already posted this question before.I got a work around but doesn't seem reasonable.

    I developed a windows service and I installed it in our server.everybod y in the office has access to a folder in the server.anybody can put a file in that shared folder and the service has to pick it up. but it does not pickup for everyone. it just picks up for some users. and the strange thing is it pickes up for rest of the users also but ocassionally.

    Iam not getting how to make it work consistantly. any ideas on this issue.

    does it have to work under admin account. if so what should i do for that.

    please help!
    Ayush
  • ayush patel
    New Member
    • Jul 2008
    • 63

    #2
    Windows service not working for everyone

    Hi
    also another question right now I have my service running under network service account. what difference will it make if I run it under local system account. I am just accessing local drives on the server no remote or network drives are being accessed so is the local system account enough to do the job?

    If anyone can tell me what is the difference between the two accounts it'll be a great help.
    Ayush
    Last edited by ayush patel; Sep 15 '08, 01:23 PM. Reason: just wanted to add some explanation

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      "I developed a windows service and I installed it in our server.everybod y in the office has access to a folder in the server.anybody can put a file in that shared folder and the service has to pick it up. but it does not pickup for everyone. it just picks up for some users. and the strange thing is it pickes up for rest of the users also but ocassionally."
      "
      Iam not getting how to make it work consistantly. any ideas on this issue. "


      Can you post a sample code... also are you using FileWatcher? What kinds of error, (in case you are getting) or exceptions are you gettin? Plz post more info and sample code...

      Comment

      • ayush patel
        New Member
        • Jul 2008
        • 63

        #4
        Thanks for the quick reply.

        yes I am using filewatcher. and the service works fine for me(Since I installed it on the server,I am asuming it'll work for me).

        there is no error,service just termiates unexpectedly for some users who try to put a file in that folder. as a recovery action i made the service to restart. when the same user tries to put the file back in the folder it works. i dont know why it stops the first time.

        Code:(text)

        1. public Service1()
        2. {
        3. try
        4. {
        5. _timer = new Timer(6000);
        6. InitializeCompo nent();
        7. }
        8. catch (Exception ex)
        9. {
        10. System.IO.Strea mWriter tw = new System.IO.Strea mWriter("C:\\gg gg\\jjjj\\error .txt", true);
        11. tw.Write(ex);
        12. tw.Close();
        13. }
        14. }




        15. protected override void OnStart(string[] args)
        16. {
        17. try
        18. {
        19. _timer.Start();

        20. FileWatch.Path = ConfigurationSe ttings.AppSetti ngs["WatchPath"];
        21. FileWatch.Creat ed += new FileSystemEvent Handler(FileWat ch_Created);

        22. }
        23. catch(Exception ex)
        {
        24. System.IO.Strea mWriter tw = new System.IO.Strea mWriter("C:\\gg gg\\jjjj\\error .txt", true);
        25. tw.Write(ex);
        26. tw.Close();
        27. }
        28. }



        29.private void FileWatch_Creat ed(object sender,
        30. System.IO.FileS ystemEventArgs e)
        31. {
        32. string[] filedata = File.ReadAllLin es(e.FullPath);
        33. int maxlins = filedata.Length ;

        34. for (int i = 0; i < maxlins; i++)
        35. {
        36. TranslateForm form = new TranslateForm() ;
        37. form.newpropert y(filedata, e);

        38. }

        39. }

        hope this provides the information.do let me know if i have to answer any questions

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Please start using [CODE] tags when you are posting your code.

          You don't need to put the line numbers there, the forum software will do that for you.

          MODERATOR

          Comment

          • PRR
            Recognized Expert Contributor
            • Dec 2007
            • 750

            #6
            when you are transfering file from network or from computer to a destination folder which has a active watcher... the ownership of file is not available to watcher as soon as file is created... so try and pause for couple of seconds in "watcher_filecr eated" ... secondly when using windows service you need to use threading so as to "come back" to onstart as soon as possible...

            Comment

            • ayush patel
              New Member
              • Jul 2008
              • 63

              #7
              Do you mean I should use "System.Threadi ng.Timer stateTimer" instead of "System.Tim ers"

              Comment

              • balabaster
                Recognized Expert Contributor
                • Mar 2007
                • 798

                #8
                Are you finding that this is inconsistent? i.e. when only one user is using the service it works all the time, but when multiple users use the service, sometimes it fails for some users, but other times it doesn't fail for those users but fails for different users? Like, does it work sometimes for any given user and then fail on other times? It seems to me that this may be a classic deadlocking issue - when the server is trying to write to the file it locks the file and you can't read it during the timespan it takes for the server to write.

                If this is what is happening, then you need to do something to alleviate this. I would write a queued read/write mechanism. i.e. you need the server to have an intermediate mechanism that can queue up reads and have clients talk to that mechanism instead of both trying to access the file directly. It works in the same fashion as the buffer on your hard disk. Files aren't accessed directly on the disk, you talk to the buffer, the buffer then gets what is on the disk and returns it to you. Likewise when the file is written, what you are writing goes into the buffer and the buffer writes to the file. This way you can alleviate this deadlocking issue that is caused by file locking.

                As an example, the queuing mechanism might have some similar interface:
                Code:
                Public Class FileInterface
                
                Private FileLockMechanism As New Object
                Private FilePath As String
                
                Private Function WriteContent(ByVal stringToAdd As String) As Boolean
                
                  SyncLock FileLockMechanism
                    'Do whatever you've gotta do to write to the file
                    'Create your writer in here...if you create it outside, it'll lock the file
                    'and then nobody will be able to read the file.
                  End SyncLock
                
                End Function
                
                Private Function ReadContent() As String
                
                  SyncLock FileLockMechanism
                    'Do whatever you've gotta do to read from the file here.  You can create
                    'the reader at the class declaration level as the reader doesn't lock the
                    'file for writing.  If you're not constantly creating and destroying the reader
                    'then you're not wasting cpu cycles unnecessarily.
                  End SyncLock
                
                End Function
                
                Public Sub New(ByVal FileName As String)
                  FilePath = FileName
                End Sub
                You'd then need to set up some kind of protocol for the client services to talk to this service. It's not particularly complex, you can use any number of mechanisms to do this. Remoting is probably the least complex to implement.

                Comment

                • ayush patel
                  New Member
                  • Jul 2008
                  • 63

                  #9
                  Thanks for the reply.
                  you are right about service being unpredictable there is also another problem with it. service terminates unexpectedly sometimes. when it restarts again it works fine. is this behavior also a deadlocking issue?

                  Comment

                  • balabaster
                    Recognized Expert Contributor
                    • Mar 2007
                    • 798

                    #10
                    Originally posted by ayush patel
                    Thanks for the reply.
                    you are right about service being unpredictable there is also another problem with it. service terminates unexpectedly sometimes. when it restarts again it works fine. is this behavior also a deadlocking issue?
                    I can't be 100% sure... the easiest method I can think to do this is by turning off the software that's writing to file for a moment... make sure all but one of the client services are shut off. Open the file in some simple application that doesn't lock the file - I assume it's a text file, so open it in notepad. Keep writing to the file and saving it and see if you can get the client service to hang or crash out. If it consistently works and doesn't fall over, then that kind of points towards locking causing the problem. Now, turn on all the client services and continue writing and saving with Notepad. They will probably all continue to function just fine. I would hazard a guess that it's the writing mechanism that's causing the issue as opposed to the client services. The client services are likely crashing because they're trying to read the file while it's locked by the server. Consequently if you push all the reads and writes through the same mechanism that streams data into the file and streams data out, then it will alleviate the problem. Alternatively, if you wrap the reads in something like this:

                    Code:
                    Dim EndTime As DateTime = DateTime.Now().Add(New Timespan(0,0,10))
                    While True And Now() < EndTime
                      Try
                        'Do stuff to read file
                        Exit While
                       Catch ex As Exception
                        'Ignore the error, we don't care
                       End Try
                    End While
                    This kind of mechanism will continue to attempt to read the file until either it was read successfully or a preset timeout passes - in this case 10 seconds. Of course, the side effect of this is that there may be unnecessary network traffic may be generated during locking events. So as we say in England, it's swings and roundabouts - you make your trade-offs and choose your approach accordingly.

                    Simple code that's quick to implement catches the exception (which is inherently expensive) and causes potentially large amounts of requests for the file over the network. But it is simple and quick and gets you up and running right now. You just need to be aware of the side effects.

                    More complex code will prevent having to catch the exception, and will require only a single network call, the server will receive the request to read which will be put into a queue and will respond once the file is unlocked. But on the other hand, now you have to code the protocol for client-server communication, which makes future code maintenance potentially more difficult.

                    Comment

                    • ayush patel
                      New Member
                      • Jul 2008
                      • 63

                      #11
                      Ok.Got it. I'll work on that and see if it works.

                      Thanks,
                      Ayush

                      Comment

                      • ayush patel
                        New Member
                        • Jul 2008
                        • 63

                        #12
                        on the contrary to what we were discussing, I just realised this service is used probably 3 times in a week randomly. none of us use it consecutively to create any deadlock problem. there is plenty of time for the system to finish reading and writing process even then the service terminates.

                        strange thing is i restart it after a minute and after restarting it works like a charm.any insight on this?

                        Comment

                        • balabaster
                          Recognized Expert Contributor
                          • Mar 2007
                          • 798

                          #13
                          Originally posted by ayush patel
                          on the contrary to what we were discussing, I just realised this service is used probably 3 times in a week randomly. none of us use it consecutively to create any deadlock problem. there is plenty of time for the system to finish reading and writing process even then the service terminates.

                          strange thing is i restart it after a minute and after restarting it works like a charm.any insight on this?
                          Hmm...can't say for sure. If it's not a locking issue, then it must be that there's some exception that's not being caught and ignored causing the service to crash. What that could be, I couldn't begin to hazard a guess without seeing all the code. What you should do is write an exception handler in the service to catch all exceptions and write them to the event log, or even just a text file so you can see why the service is crashing. Chances are, by doing that, it'll stop crashing, but you should be able to see the errors that were causing it to crash previously, they'll just be handled now.

                          Comment

                          • ayush patel
                            New Member
                            • Jul 2008
                            • 63

                            #14
                            I tried catching the exception. even the start and stop of service are in try catch blocks. but no exception is recorded since the service just terminates without executing any code.

                            Comment

                            • balabaster
                              Recognized Expert Contributor
                              • Mar 2007
                              • 798

                              #15
                              Originally posted by ayush patel
                              I tried catching the exception. even the start and stop of service are in try catch blocks. but no exception is recorded since the service just terminates without executing any code.
                              Are all the methods in all the classes in try/catch blocks? The start and stop are only fired when the service starts or stops, not as you noticed when it crashes. So we need to pinpoint where it is crashing which will require you digging through the methods in each class... and perhaps even the properties.

                              Comment

                              Working...