Options

File Locking and Web Services

mrNavmrNav Member Posts: 10
edited 2009-01-30 in NAV Three Tier
Hi anyone know any better about this problem:

I sent this to support, and got the response below which errr kind of answered the "question"


My Support Request to MS:

Thanks for that, but there is a problem with this model:

If we have a web service which calls a codeunit which must log to a file.

We are finding that every so often the web service falls over, saying it can’t access the log file in question, presumably because the file is in use by another thread, which may have got the file lock and writing to it.

If we can’t use SingleInstance Codeunit to communicate across the threads then how do we prevent this file locking issue?


Example:

Webservice()
{

WriteToLogFile(‘log this’);

}



WriteToLogFile(str logmsg)
{
f.Openfile(“c:\file.log”);
f.Write(logmsg);
f.Close();
}



When writing to the log we are opening and closing the file as quickly as possible, however because of the concurrency and lack of communication between threads which apparently is not possible how do we prevent this problem?, I guess we could use a table as a semaphore to record access to the log file, but this is not ideal.



Support Response ->

Thanks for your clarification.

In a way, your example proves that web services really are multithreaded, which is the good news :).

You would need to find a way to lock the file, or some ways for multiple processes to write to the file at the same time. Each thread is completely separate so I am afraid you won’t be able to share the file.

Hope this helps. Waiting to hear fro you.

Best Regards,

Some Guy at MS. ( Not marks and spencers! )

Comments

  • Options
    DenSterDenSter Member Posts: 8,304
    I don't think single instance codeunits work across connections to web services, so that's one thing you should reconsider. Instead of writing to a log file, I would consider writing a record to a log table, and create a report off of that table for analysis purposes.
  • Options
    freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    Maybe a stupid question - but why don't you log to a table?
    If you need to log to files - you have a log file for each Web Service Listener - and then put a timestamp in the log (if you need to merge the log's later).
    Trying to have different processes waiting for each other seams only to hurt performance and not really help anybody.
    Freddy Kristiansen
    Group Program Manager, Client
    Microsoft Dynamics NAV
    http://blogs.msdn.com/freddyk

    The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.
  • Options
    mrNavmrNav Member Posts: 10
    Yes been told my support that Single Instance Codeunits are re-created inside each web service request as seperate instances, they cannot communicate between Threads.

    And therefore yes the table logging method seems to be the way, the issue is really is if you have a lot of legacy code which already logs to the file system then you have your work cut out.
  • Options
    ara3nara3n Member Posts: 9,255
    You can use

    if SETSTAMP(filename,newdate,newtime) then begin

    end

    to see if the file is locked. and wait till it is released. but in a sense you are making the file be your semaphore.
    I would rather use events, send email, create separate files for each connection.
    afterward at the end of the have a processing that will combine the files.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
Sign In or Register to comment.