Wednesday 15 February 2012

c# - Locking issues with replacing files on a website -


I want to replace existing files with an updated version of an IIS website. Say these files are large PDF documents, which can be accessed through hyperlinks. The site is up to 24x7, so I'm concerned about locking problems when a file is being updated at the same time, when someone is trying to read the file.

The files are updated on the server using the C # code running. I can think of two options to open the file to write.

Option 1) Open the file to write using FileShare. Read:

 using  (file stream stream = new file stream (path, file mod. Creat, filex, fileshore. If this file is open, and a user through a hyperlink In the web browser, requesting the same file to read, the document opens as a blank page.  

Option 2) Open the file to write using the fileshare.NONE: Using

  (File streams) Rim = new filestream (Path, FileMode.Create, FileAccess.Write, FileShare .No))  

Although this file is open, and a user accesses a web browser through a hyperlink In the same file requests to read, the browser shows an error In IE 8, you get HTTP 500, "Can not display website page", and in Firefox 3.5, you get: "Process file Can not access because it is second The process is being used by the process. "

The nature of the browser behavior is understandable and it looks reasonable. I suppose it is not highly likely that a user is attempting to read a file at the same time What you are updating it It would be good that in some way, the file update was atomic, like updating a database with SQL wrapped around a transaction.

I am thinking that you guys worry about such things, and the options given above to update the files, or you have other options too

< / Div>

How to copy the new version of the file with a different name and then the overwritten argument true < / Em>? When you are writing this, you will not interfere with the web server and will be quick to move the file.


No comments:

Post a Comment