File Locking and Web Services

mrNav
Member Posts: 10
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! )
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! )
0
Comments
-
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.0
-
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.0 -
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.0 -
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.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions