Opening a Memory Mapped File causes FileNotFoundException when deployed in IIS

浪尽此生 提交于 2019-12-01 16:46:50
Toma Tacescu

Add "Global\\" prefix to the mapName. That is the only way it worked for me. When we try to access shared memory created in the first process it runs in a different security context and shared memory objects are not visible unless they are created in the global namespace.

This is the code that worked for me. I used CreateOrOpen in a WinForm application and I used OpenExisting in an IIS process:

mSec = new MemoryMappedFileSecurity();
mSec.AddAccessRule(new AccessRule<MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow));

mmf = MemoryMappedFile.CreateOrOpen("Global\\\MmfName", 100, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, mSec, HandleInheritability.Inheritable);

mmf = MemoryMappedFile.OpenExisting("Global\\\MmfName");

If Inter Process Communication (IPC) is what you are trying to achieve, you could use WCF with a Named Pipe Binding. You gain a lot of simplicity at the cost of some performance.

For non physical file, there are two cases,

  1. File is deleted after your process exits.
  2. IIS does not run in same user as your console process, IIS runs in little restricted mode, so unless you create File with added security to allow process to access your service.

You will have to use http://msdn.microsoft.com/en-us/library/dd267529(v=vs.110).aspx this constructor with following security.

        MemoryMappedFileSecurity mSec = new MemoryMappedFileSecurity ();
        mSec.AddAccessRule(new AccessRule<MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
            MemoryMappedFileRights .FullControl, AccessControlType.Allow));

And then try to access it from IIS with same security.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!