SharedMemory icon indicating copy to clipboard operation
SharedMemory copied to clipboard

When I run shared memory within a window service, Unable to read from applications as console application

Open vinaydwivedi opened this issue 5 years ago • 11 comments
trafficstars

private SharedMemory.SharedArray<MarketTickS> _buffer = _buffer = new SharedArray<MarketTickS>(@"Global\MARKETTICK", 200000); from within window service.

Try creating SharedMemory.SharedArray<MarketTickS> _reader = new SharedArray<MarketTickS>(@"Global\MARKETTICK");

I Get "Access to the path 'Global\MARKETTICK_evt_write' is denied"

When I create the file without Global Prefix i.e. private SharedMemory.SharedArray<MarketTickS> _buffer = _buffer = new SharedArray<MarketTickS>("MARKETTICK", 200000); from within window service.

from Console SharedMemory.SharedArray<MarketTickS> _reader = new SharedArray<MarketTickS>(@"MARKETTICK");

I get file not found error.

vinaydwivedi avatar Sep 02 '20 09:09 vinaydwivedi

What is the window service authenticating as? I would say you need to change this.

justinstenning avatar Sep 02 '20 09:09 justinstenning

Window service is authenticated as Network service, tried running it under local service account as well, doesn't work.

vinaydwivedi avatar Sep 02 '20 10:09 vinaydwivedi

I believe the service must be running using an account that has the Local Security Policy “Create global objects” allowed.

justinstenning avatar Sep 02 '20 10:09 justinstenning

Yes, please see below, Just to be sure, if this is not permission issue, I granted create "create global objects" to everyone.

image

vinaydwivedi avatar Sep 02 '20 10:09 vinaydwivedi

Could following be required, don't see this being set.

var security = new MemoryMappedFileSecurity(); security.AddAccessRule(new AccessRule<MemoryMappedFileRights>("everyone", MemoryMappedFileRights.FullControl,AccessControlType.Allow));

vinaydwivedi avatar Sep 02 '20 13:09 vinaydwivedi

Probably for the client to connect but that does not explain why you can’t create the initial MMF.

justinstenning avatar Sep 02 '20 23:09 justinstenning

Both the issue were from client side. I have switched to Managed version of MemoryMapped files, and able to work as expected with security MemoryMappedFileSecurity added.

vinaydwivedi avatar Sep 03 '20 04:09 vinaydwivedi

Ah I thought the issue was creating not opening existing. Anyway, sounds like that is the issue, thanks.

justinstenning avatar Sep 03 '20 04:09 justinstenning

Hey, vinaydwivedi. Can you explain your solution more detailed? I have similar issue but with CircularBuffer, between service (net47) and consol (net35)

lukiluki123 avatar Nov 04 '20 12:11 lukiluki123

Hey all, bumping this issue thread again because we are also running into this issue.

SharedMemory via RpcBuffer works fine when the host & client are console applications, but no longer when the host is a Windows Service.

I tried the Global\Name approach, but this is not a feasible solution for us as we are deploying to hundreds of machines and would rather not modify permissions for all users on these machines just for sharing a mutex lock.

I also tried cloning the project, adding MutexSecurity as a parameter to RpcBuffer and using it to initialize the Mutex. However, I still ran into the same issues (probably due to a lack of knowledge about how to properly configure this).

Is there anyone in this thread who is willing to post their solution?

wsloth avatar Jun 28 '21 07:06 wsloth

I haven’t had a chance to look at this in a lot of detail yet, but the MutexSecurity looks like the correct approach to me.

It looks like you need to use a security descriptor with null DACL (might not be possible using current managed classes) - take a look at the following (old but might provide some direction) https://www.codeproject.com/Articles/14022/A-security-neutral-mutex-class-for-the-managed-pla

justinstenning avatar Jun 29 '21 12:06 justinstenning