File protection between webservice calls

N

nano2k

Hi
My C# webservice uses a file to store some (short) information between
calls.
One method creates/updates the content of a file, another reads it.
What is the best method to avoid issues like "file is already opened by
another process", etc between calls?
Using Monitor.Enter(...) and Monitor.Exit(...) (or lock statement in
C#) will gracefully solve my problem in every situation?

Thanks.
Adi.
 
N

nano2k

Hi

Thanks for your response.
Below is a code snipet from one of my webservice methods:

string fName = Path.Combine(path, "schemaDate.dat");
lock(m_syncToken) {
StreamWriter sw = File.CreateText(fName);
try{
sw.Write(m_schemaDate.ToString(CultureInfo.InvariantCulture));
}
finally{
sw.Close();
}
}

m_syncToken is defined as:
private static object m_syncToken;

My question is: is this pattern powerful enough to ensure exclusive
acces to only one request? I'm using .NET Framework 1.1 and IIS 5.1.


Mariano Omar Rodriguez a scris:
 
M

Mariano Omar Rodriguez

This code only assures single access to the file if only a worker process is
running at the time.
Ahother thing is that you need call Dispose instead of Close.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top