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:
- Start uploading the chunk
- Loose internet connection during upload
- UploadChunk methods throws
CommunicationException
because of the lost internet connection - ...wait for internet connection to come back
- Start uploading the last chunk again
- 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.