File protection between webservice calls

Discussion in 'ASP .Net Web Services' started by nano2k, Jan 22, 2007.

  1. nano2k

    nano2k Guest

    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.
     
    nano2k, Jan 22, 2007
    #1
    1. Advertising

  2. You'll need execute the Dispose method instead of close.

    "nano2k" <> wrote in message
    news:...
    > 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.
    >
     
    Mariano Omar Rodriguez, Jan 22, 2007
    #2
    1. Advertising

  3. nano2k

    nano2k Guest

    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:
    > You'll need execute the Dispose method instead of close.
    >
    > "nano2k" <> wrote in message
    > news:...
    > > 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.
    > >
     
    nano2k, Jan 23, 2007
    #3
  4. 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.

    "nano2k" <> wrote in message
    news:...
    > 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:
    >> You'll need execute the Dispose method instead of close.
    >>
    >> "nano2k" <> wrote in message
    >> news:...
    >> > 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.
    >> >

    >
     
    Mariano Omar Rodriguez, Jan 24, 2007
    #4
  5. nano2k

    nano2k Guest

    Dispose method of what object?


    Mariano Omar Rodriguez a scris:
    > 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.
    >
    > "nano2k" <> wrote in message
    > news:...
    > > 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:
    > >> You'll need execute the Dispose method instead of close.
    > >>
    > >> "nano2k" <> wrote in message
    > >> news:...
    > >> > 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.
    > >> >

    > >
     
    nano2k, Jan 24, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. g
    Replies:
    69
    Views:
    2,196
    Oliver Wong
    Apr 25, 2006
  2. Honne Gowda A
    Replies:
    2
    Views:
    880
    Karl Heinz Buchegger
    Oct 31, 2003
  3. andy6
    Replies:
    2
    Views:
    762
    andy6 via DotNetMonster.com
    Jun 9, 2006
  4. K. Jansma
    Replies:
    7
    Views:
    328
    Duncan Booth
    Feb 21, 2006
  5. Richard Tobin
    Replies:
    24
    Views:
    793
Loading...

Share This Page