Where to place a frequently accessed file in an ASP.NET application? - asp.net

I am writing an ASP.NET Web Form application.
Each time a request arrives my aspx page, I will use an object to do some logic. This object is deserialized from a pre-made file.
There'll be many requests to my aspx page so I don't want to deserialize this object every time a request comes in. Becuase as I remember, ASP.NET will serve different requests with different threads. And the one thread reading the file will block other threads from reading this file. So hurt the performance. (But I am not sure about this, please correct me if I am wrong.)
The file will be updated weekly or bi-weekly. So I need to re-deserialize the corresponding object once the file is updated.
My questions are:
Where should I put the code to deserialize the object from file?
How to monitor the file change?
My current plan is:
Deserialize the object from file at the Application_Start() event. And store this object in Cache.
Use FileSystemWatcher to monitor the folder containing the file and update the object once file is modified.
Is this design OK? Is there any more reasonable design?
Thanks.

Store the file in the App_Data.
FileSystemWatcher is ok but I would suggest it to run as a windows service. You could have a page dedicated to refresh the cache (ex: www.webpage.com/refreshcache.aspx). You could also just setup a scheduler to refresh the cache every night.

Related

make a non-routing link in asp.net MVC 3

I'm trying to include a link in my MVC 3 project to a file in the App_Data folder, but the MVC always wants to make the link <controller>/<view>/App_Data/file. Unfortunately App_Data is not the lowest folder in my web server, so I can't use just /App_Data/...
Is the trick to muck with the routing table?
Read the file in app_data into temp memory using system.io methods, clear the response buffer from a new action in a controller, (ensure the controller returns void). The once read in, write the file directly to the output stream of the response object. Remember to set the content type on the response object to help the client machine read/understand the file.
Hope this helps.
Chris.

How to read files in a concurrency safe manner in an asp.net webpage?

I need an asp.net webpage to read some text from a file stored on the same server as the webpage, will the following code result in a concurrency error if several people visit the webpage at once and thus several requests to read the file are generated:
string fileText = System.IO.File.ReadAllText(Request.PhysicalApplicationPath + #"Folder\File.txt");
If it would result in an error, what would be a concurrency safe manner to read a file from an asp.net page?
Thanks.
Based on your comment, read it on application start event from global.asax file and put it in to application level variable. So whole of the application users can access without any issue,

Not able to access session in Httpmodule for static resources like css /js/ image files

I have created a HTTPModule. Session information is retrieved using PreRequestHandlerExecute. It is working well for aspx pages. But while loading css/js or any image files, Session is null.
I have gone through the link ASP.NET CSS file not loaded when adding HttpModule to web.config
But could not get the implementation.
Anyone has any idea about this?
I think you'll find that your static files are being handled by the StaticFileHandler and as such wouldn't fire off session events as the session won't be being activated for efficiency purposes.
You could configure IIS to pass static files through a different handler (the PageHandler for example) but really you'd need to know if that was required first. You haven't described why you want sessions to be available for static files.

Flash uploader and ASP.net MVC

I have a flash upload component I want to use to upload multiple files. I'm using it in a MVC app and what I want to happen is that the user picks the files they want to upload, it uploads them and then displays a page showing all the files they have uploaded so they can add a description and select where to save them, and then save the files.
At the moment when files are uploaded the flash component calls a controller to process the files, this bit works fine, I can get the uploaded files and do what I like with them. The problem is is that I cannot just redirect to a View once the controllers done its work, because its the flash component calling the controller, not the page and so nothing happens when you try and do that.
I had attempted to save the files in the session and then forward the user on completion of the upload using some code in the flash actionscript, this however does not work, the session always turns up null. I had also considered actually saving the files to a temp location and then on the displaying page just listing all files in the temp location, but this is then going to involve saving the files twice, once to the temp directory and then to the actual place the user wants to put them, which I assume will be slow.
Any thoughts on the best way to do this?
Is your site using cookie based authentication? If so then the flash uploader needs to include the authentication cookie when uploading otherwise the upload will be seen as coming from a new user - this would explain your null values in the session state. If you are unable to get flash to post the cookie then you'll have to identify the user within the upload URL.
You should keep session state to a minimum or even better not use is at all so storing large amounts of data such as images in it is a bad idea.
With our applications we save all uploaded files to the database and then give them a unique Guid that is then used to retrieved/display them later. Within the database images could be associated with a user and in your case be marked as just uploaded so that when you redirect the user to the additional information page you know which images to display.
but this is then going to involve
saving the files twice, once to the
temp directory and then to the actual
place the user wants to put them
In relation to where the files are saved on the server you should not be allowing the user to determine where the files are saved.

ASP.NET FileUpload control not working inside Wizard control

I have a Wizard control with one of the pages containing a FileUpload control. I want to access the file stream at the point of the final page of the wizard but this doesn't seem to work.
As far as I can see, you can only access the file stream for the posted file on the postback which occurs immediately after the control has been used. As the file will ultimately be put into a DB record I could save it at this point, but I'd rather avoid this if possible.
Does anyone know of a workaround for this problem?
I've not used this control myself but as it's based around the HTML standard INPUT control I think you are stuck with having to receive the file on the post-back that processes that control.
The difficulty in circumventing this would be around how to pull the file from the client system when you are effectively sandboxed from the local filesystem, hence the need for the upload control.
I would suggest that you store the file into your DB blob (or as a temp file on the filesystem, probably need a unique filename; I find a GUID works nicely) and then use the Session object to retain that reference to the end of the process. The only other alternative I can think of that doesn't involve major work would be to move the file upload to the last page of your wizard.
You could have the displaying of your individual wizard stages managed client-side using CSS/jQuery rather than keep POSTing back to the server? That way, your only POST (resulting in the upload) will be at the end of the Wizard.

Resources