3

I have a wcf method like this for uploading file chunks:

public void UploadChunk ( RemoteFileChunk file )
{
    using ( var targetStream = new FileStream("some-path", FileMode.OpenOrCreate, FileAccess.Append, FileShare.None) )
    {
        file.Stream.CopyTo(targetStream);
        file.Stream.Close();
    }
}

Which is pretty basic stuff. But what happens on an exceptional case is pretty strange. Exceptional case steps:

  1. Start uploading the chunk
  2. Loose internet connection during upload
  3. UploadChunk methods throws CommunicationException because of the lost internet connection
  4. ...wait for internet connection to come back
  5. Start uploading the last chunk again
  6. Boom!!! Throws the exception below:

The process cannot access the file 'some-path' because it is being used by another process.

I know that file is not touched by anyone else, which leads me that the file was left open on the last call when connection lost. But as far as I know using statement should have closed the FileStream, however in this case it didn't.

What I might be missing here?

Btw, I have another question which I'm guessing is caused by the same problem that I'm not aware of. Maybe it can lead you guys to some clue.

Community
  • 1
  • 1
Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
  • because your original streaming operation isn't done. – T McKeown May 06 '15 at 13:34
  • Has the original execution of `UploadChunk` failed when you re-try? Network timeouts can be longer than you expect. – Richard May 06 '15 at 13:34
  • I assume you are encountering an exception. In this case you should surround your code in a try-catch-finally block and close the stream whatever unexpected happens. – greenhoorn May 06 '15 at 13:35
  • @Richard and @T McKeown I have added the 3rd step. The first call finalizes with a `CommunicationException`, then after internet connection is back I make the second call. – Tolga Evcimen May 06 '15 at 13:44
  • @Richard, when you said timeouts I gave it a shot, and waited for a little bit longer(2-3 mins), now it can make the call succesfully. But it is still wierd since the first call is actually done with an Exception, and my openTimeOut value on service and client is 5mins. – Tolga Evcimen May 06 '15 at 13:58
  • Depending on how your WCF service is defined, I think you get a new instance of the service class when the reconnection is made, which means that there are probably 2 instances running, which are both trying to append to the same file. – Maarten Sep 28 '16 at 08:49

1 Answers1

1

What is RemoteFileChunk? My guess is that it is RemoteFileChunk that has the file open. You have not shown any code for RemoteFileChunk which demonstrates that it will automatically close its Stream when an exception occurs. This should work (although it might be better to encapsulate the close within RemoteFileChunk itself):

public void UploadChunk ( RemoteFileChunk file )
{
    using ( var targetStream = new FileStream("some-path", FileMode.OpenOrCreate, FileAccess.Append, FileShare.None) )
    {
        try
        {
            file.Stream.CopyTo(targetStream);
        }
        finally
        {
            file.Stream.Close();
        }
    }
}
Polyfun
  • 9,479
  • 4
  • 31
  • 39