c# - How to make Memory-Mapped files in a web application visible to all threads -


i have following code entry point memory-map file, in other words first request server, writes input string mm file can available 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, have following code used other requests, can read 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                     {                         // sth                     }                     catch (exception e)                     {                         logger.addentry(s, e);                         return false;                     }                 }             }         }         catch (ioexception e)         {             logger.addentry(e);             return false;         }         return true;     } 

but when last function tries open mm file gets ioexception: file not found.

what doing wrong, thought mm files worked shared memory mechanism in .net, or missing in code?

without seeing how setinput or getinput called, have make assumptions.

are synchronising calls getinput , setinput? if request comes in , try map named region before first request has finished creating it, you're going have problems. you'll want create shared mutex synchronise access file.

also, depending on how web requests handled, process handling first request may have died time second request comes in, release handle held first call memorymappedfile.createoropen, resulting in os releasing file. data not persisted because file not backed filesystem.

try create filesystem-backed shared memory region, using createfromfile , see if still problem.


i question direct use of shared memory, though. don't know project, there other mechanisms achieve ipc. depending on particular case, might find dbms easier work (even small 1 sqlite).

if it's message passing want, there plenty of existing options ease pain scenario too. see this question, , this one. think you'll have luck wcf.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -