none
[Service] Strange behavior / bug at writing a file RRS feed

  • Question

  • Hello everybody,

    I've got a Windows-Service hosting a WCF-Service for our frontend UWP-Application. There is a simple WriteFile-Method, wich accepts a path and a byte[] as Parameters. All it simply does, is call File.WriteAllBytes(). Sometimes customers report me errors at the next time, they start our UWP-Application, wich simply calls a ReadFile-Method on our WCF-Service (it just calls File.ReadAllBytes() and Returns the byte[]). I've had a look into the file and saw, that it had the right size, but just 00 bytes in. I can't reproduce it myself, but I got some customers having this issue. Do you have any suggetions, what can be wrong? Maybe it's caused through shutdown of the machine, causing the shutdown of the Windows-Service?

    Best regards,
    Steve

    Here is my WCF-Code:

    public byte[] ReadFile(string pPath) { try { return File.ReadAllBytes(pPath); } catch (Exception ex) { LoggingService.Current.Log(ex); throw ex; } } public void WriteFile(string pPath, byte[] pContent) { try { return File.WriteAllBytes(pPath, pContent); } catch (Exception ex) { LoggingService.Current.Log(ex); throw ex; } }

    Wednesday, May 9, 2018 3:54 PM

All replies

  • I doubt the issue is with the code you posted. You're talking about passing data from a UWP app to a WCF service. That means the problem could be in what the UWP app sends, the binding being used, or the service call itself. When passing files I think about issues around streaming vs buffering, length of the file, bad bindings (e.g. using MTOM on one side but not the other, using streaming vs buffered operations, WS HTTP or NET TCP). The list goes on and on.

    I would recommend that you add some logging to your WCF service method. If it is getting data it doesn't expect then log an error along with the payload information so you can diagnose the issue. It is also a great place to go ahead and fail the call so it doesn't get written out that way.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 9, 2018 5:14 PM
    Moderator
  • Hi Michael,

    thank you for your quick response. The logging was the first think I thought about, so I've already added it to the service. But I'am still confused about the file size, wich is exact the size it should be. I using the buffered mode and the biggest file, I'm expecting is about 5MB, so performance shouldn't be an issue. I'm currently using simple XML with Base64 encoded binary data, but I think MTOM is a good idea. The WebService is a HTTP WebService.

    Thanks!

    Best regards,
    Steve

    Edit:
    MTOM isn't an option, because it currently isn't supported on UWP.


    Friday, May 11, 2018 8:31 AM
  • So when you say the file size is correct that means you're getting the entire byte array on the server side for writing. When you look into the array however it is all zeroes? If so then the issue is on the client side or the binding. If you get the file and the array contains the correct data then verify the contents after it is saved to disk.  If the file on disk is correct but the client is getting a corrupted file back then reverse the process.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, May 11, 2018 2:35 PM
    Moderator
  • Hi Michael,

    yes, the file size is correct but the file itself only contains zeros. I don't even think, it's the binding. I've got one case, where the service writes an own file (without the client and without any binding) and one customer reported me right now an error with this file. I've had a look into it and only saw zeros, but again the file size was correct. The service is running on a tablet pc and is shutdown randomly (maybe it's writing at the time of shutdown). Do you think, this can be an issue? I'm already handling the OnStop event of the service and stop my work, when get called. Do you have any idea, how to wait for file-operations to finish?

    Best regards,
    Steve

    Wednesday, May 30, 2018 2:06 PM
  • If you're writing XYZ to a file but you see 00s in the file then it could be a flushing issue. To work around that you could force the flush. This is generally done by simply closing the file. But if you are leaving the file open continuously then a sudden stop would potentially lose data. You might need to periodically force a flush using the Flush method (IIRC).

    Provided you're closing your files before OnStop completes then the files will be flushed before your service stops. Even if they weren't then Windows would finish flushing before it released the handle.

    You may benefit from running process monitor or similar program and watch the file IO go by to see if your app is sending the right data and verifying that it is getting flushed to disk properly.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 30, 2018 2:45 PM
    Moderator
  • Hi Michael,

    thanks for your quick reply. I'm already flushing and closing the stream at the end of each write operation. Here's my code:

    internal static void WriteAllBytes(string path, byte[] bytes)
    {
      using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
      {
        fs.Write(bytes, 0, bytes.Length);
        fs.Flush();
        fs.Close();
      }
    }

    I thought about implementing some kind of transactional file writer, wich works with Transaction.EnlistVolatile and  Commit and eventualy Rollback. But this error is very hard to test, because I can't debug and simulate at the same time (because, when I shutdown the tablet, my debugging will stop).

    Best regards,
    Steve

    Thursday, May 31, 2018 9:21 AM
  • That does seem odd. I would be shocked if Windows wasn't flushing all the file buffers before shutdown though. You'd likely be seeing issues elsewhere as well. Given that you're using WriteThrough then even the flush is redundant at this point.

    Can you narrow down to the issue happening only if Windows is shut down? If so then you might turn on verbose logging during shutdown. It is possible that your service is taking too long to shut down and it is simply being terminated. The event logs may help with that information as well.

    Another possibility is that you're getting all 00s. Did you add a check in to look for this special case? 

    Something else to consider is that you are hosting a WCF service which means you have a dependency on the network subsystem. Did you set up dependencies in your service so it won't start/stop until the networking subsystem is available?


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 31, 2018 1:49 PM
    Moderator