문제

i have the following code which is the entry point for the Memory-Map file, in other words this the very first request to the server, which writes the input string to the MM file so it can be available to other requests later:

    internal string SetInput(string input)
    {
        try
        {

            MemoryMappedFile mmFile = MemoryMappedFile.CreateOrOpen("Map",2048,MemoryMappedFileAccess.ReadWrite);
            using (MemoryMappedViewStream vStream = mmFile.CreateViewStream())
            {
                BinaryWriter bw = new BinaryWriter(vStream);
                bw.Write(input);
            }
            Logger.AddEntry(Logger.Level.Inf, input);
        }
        catch (Exception e)
        {
            Logger.AddEntry(input, e);
            return "W";
        }
    }

Now, i have the following code be used with other requests, so they can read what the first request sent:

private bool GetInput()
    {

        try
        {
            using (MemoryMappedFile mmFile = MemoryMappedFile.OpenExisting("Map"))
            {
                using (MemoryMappedViewStream stream = mmFile.CreateViewStream())
                {
                    BinaryReader reader = new BinaryReader(stream);
                    string s = reader.ReadString();
                    try
                    {
                        // Do sth
                    }
                    catch (Exception e)
                    {
                        Logger.AddEntry(s, e);
                        return false;
                    }
                }
            }
        }
        catch (IOException e)
        {
            Logger.AddEntry(e);
            return false;
        }
        return true;
    }

But when this last function tries to open the MM file it gets a IOException: file not found.

What am i doing wrong, i thought MM files worked as a Shared Memory mechanism in .NET, or am i missing something in my code?

도움이 되었습니까?

해결책

Without seeing how SetInput or GetInput are called, I have to make some assumptions.

Are you synchronising the calls to GetInput and SetInput? If a request comes in and you try to map to the named region before the first request has finished creating it, you're going to have problems. You'll probably want to create a shared mutex to synchronise access to the file.

Also, depending on how your web requests are handled, the process for handling the first request may have died by the time the second request comes in, which will release the handle held by the first call to MemoryMappedFile.CreateOrOpen, resulting in the OS releasing the file. The data will not be persisted because the file is not backed by the filesystem.

Try to create a filesystem-backed shared memory region, using CreateFromFile and see if you still get the problem.


I question your direct use of shared memory, though. I don't know anything about your project, but there are other mechanisms to achieve IPC. Depending on your particular case, you might find a DBMS easier to work with (even a small one like sqlite).

If it's message passing you want, there are plenty of existing options that will ease your pain for that scenario too. See this question, and this one. I think you'll have luck with WCF.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top