In my WCF service, I need to provide file upload functionality with support for the Range HTTP header for batch upload. The first call to the GetFile service GetFile creates a new instance of MemoryMappedFile from a file on disk. Suppose that at a time when the MMF created by the first request and this request is still being processed, the second call to the GetFile method GetFile open the existing MMF and return a stream response to the client. What happens if the MMF is deleted (and the source file is closed when the MemoryMappedFile is disposed) by the thread that creates it? Should the second call successfully read all content from an already open ViewStream or not?
I wrote a small test and, it seems, before opening the MemoryMappedFile using the OpenExisting method, it is extended for life and the source file remains open. Is this true, or did I miss some trap? I can not find the documentation for this case on MSDN.
Update : added an additional call to Thread.Sleep after opening the MMF until receiving a MapView file to simulate a stream race.
private static readonly string mapName = "foo"; private static readonly string fileName = @"some big file"; static void Main(string[] args) { var t1 = Task.Factory.StartNew(OpenMemoryMappedFile); var t2 = Task.Factory.StartNew(ReadMemoryMappedFile); Task.WaitAll(t1, t2); } private static void OpenMemoryMappedFile() { var stream = File.OpenRead(fileName); using (var mmf = MemoryMappedFile.CreateFromFile(stream, mapName, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, false)) { Console.WriteLine("Memory mapped file created"); Thread.Sleep(1000);
source share