Reputation: 8327
I have a VB.NET web app frontend and a VB.NET windows app backend, both running on my Azure Windows 2012 server VM.
The backend runs every 10 seconds, review the data files, and sometimes writes data.
The frontend (web) accesses the data files when a user is using the web.
Sometimes, the web gets an error when it does My.Computer.FileSystem.WriteAllText or My.Computer.FileSystem.ReadAllText.
These two methods are how both backend and frontend access the data files. Data files are all simple .txt text-based files.
Upvotes: 1
Views: 99
Reputation: 2423
Judging from the fact the failures are intermittent, it's likely that you are experiencing contention issues with your two processes. You might want to manually write to the file and ensure you implement some sharing that will at least mitigate some of the issues:
Using fs As New FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read),
sw As New StreamWriter(fs)
sw.Write("A whole bunch of text to write")
End Using
The above code opens the file after placing a read only lock on the it. This allows subsequent processes to open the file to read so even while you are holding this file for instance in your Windows app, your web app can read what's in it without throwing.
Using fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
sr As New StreamReader(fs)
Console.WriteLine(sr.ReadToEnd())
End Using
This last snippet is what you use for reading the file. It places a read/write share when it opens. Since this process is only reading from the file, you can allow other processes to open it for reading as well without issue. In this case, I allowed other processes to write to the file so that they are not restricted.
You'll have to handle exceptions that may arise when you cannot open it in the mode you specified because of a lock that was set up by another process.
Upvotes: 1