An idea about deleting junk files (like attachments) regularly - asp.net

I've created an ASP.NET web form site.
The site allows the user to write articles, and upload attachments as well.
While the user is writing the article, any uploaded attachments are moved into a temp folder that I created in my site folders, and just when the user submits the article, the attachments are moved to an appropriate path,
But what if the user closed the form, discarding the article? are files in the temp folder will be permanent? of course not!
SO
I want to determine a good scenario for deleting the temp files regularly, without deleting any files that are in pending.
I'm thinking about a scenario:
Deleting any files that are created for more than an hour! (using file system functions) and that occures in the Application_Start event or Session_end
Any better ideas?

I would do it as a stand-alone Windows Service.
If you add that logic to an Application_Start event, if your site is constantly visited (and never recycled by IIS), you'll only run that process once. You'll also have the potential to dramatically slow the initial precompile of the site for the first visitor.
Session_end could be good, but if two people have it end at the same time, you could run into a race condition.
A Windows Service gives you the benefit of always running (and not using CPU when waiting, if written correctly), and it doesn't interfere with the working of your ASP.NET site.

Related

Avoid application reset when deleting a folder

I'm having a bit of trouble with an ASP.Net MVC project.
The users can upload images, which are stored with an ID in the database, and the files are then stored in /Files/Images/ID/
Upon deleting the images again, I delete the folder /Files/Images/ID (that's the ID folder being deleted)
However when deleting a folder under the project, the application immediately resets, thereby losing all session and throwing all my users off.
Is there any way to mark a path so ASP.Net doesn't reset upon changes being made to the path ?
It's very simple. Your ASP.NET application should not be performing file write or modify operations within the paths of your ASP.NET application. Use a different path for where you store the uploads.

How can I replace my site's dlls without loosing session data (prevent application restart)?

When I modify my code I have to upload the project's DLL I modified it, but the problem is that I loos data I put in session because my application restarts.
Is this normal? or I should do something to prevent the restart?.
I feel that this is wrong because imagine if you're working on shopping site and people buying goods and suddenly they lost every thing they put in the shopping cart (because you save their choices in session.
Is there a way to do this in Asp.net?
Thanks
From you question it looks like you are using sessions in proc (that is, in the IIS process).
This means that whenever IIS restarts you would indeed lose session data (as would be the case if you update the DLLs in the bin folder).
To solve this, there are several options:
move session information to SQL Server
move session information to a dedicated session server
remove all dependencies on sessions from your application.
See the MSDN documentation of the sessionState configuration element.

Do changes to an asp page in classic asp require an iisreset?

If I make a change to the vb code in a classic asp page does the change get picked up automatically or is an iisreset needed?
Thanks
Quickest answer NO you do not need to reset IIS
Once the changed file is saved, the new code will run.
There's no need to reset IIS or build the project.
The ASP Script engine does maintain a cache of "compiled" scripts (where the results of parsing and tokenizing etc are stored. So that subsquent requests for the same ASP page can be processed more quickly. However the last modified date of the ASP file forms part of the cache identity of the cached page. Hence if the page has changed since the last request the cached item is dropped and a new one built when the next request arrives so it all works seemlessly.
So as the others very quickly said, you don't need an IISReset or even an App pool recycle.
It may be worth pointing out that as of IIS6 there are very very few circumstances where you would ever need to perform an IISReset. IISReset is massively draconian and high impact. Most of the time when such a "reset" is needed a simple re-cycle of the appropriate application pool will do which has a far more gentle touch.
Even back on IIS5 a close equivalent of a app pool recycle could be achieved with by restarting the appropriate COM+ application.
On Win 2003 Server (IIS6) most of the time, save the changes to the file and it works.
I have had caching problems when the files have been saved and then copied/moved to the final location which is a virtual folder in IIS.
For example:
Say C:\inetpub\wwwroot\myfolder\ is the physical path for the URL http:Myserver/myApp/
I save my files in C:\inetpub\wwwroot\test\ and everything works fine
Move/copy the files from 'test' to 'myfolder' overwriting existing files and when I access http:Myserver/myApp/ I see my old pages, not the updates.

Remove temporary files web application

I'm developing a Web application, and there is a page when user must submit files which are saved into temporary folder on the server. If everything goes well, I send ajax request to the server to remove users uploaded file. But, if user closes the browser or shutdown the computer, I can't detect that.
In this case, what is the best way to gurantee that unused files are not stored forever? The site is developed in ASP.NET MVC and will be hosted on II7. Does IIS7 provides some configuration to deal with temporary files? Or I need to implement some service, which will be executed in a background with low priority and periodically check if there are "old" files to be removed?
Any help is very much appreciated.
I would have a windows service in the background to delete old files at some interval.

Re-publishing an ASP.NET Web Application While Site is Live

I am trying to get a grasp on how to handle updates to a live, functioning ASP.NET (2.0 or greater) Application while there are users on the site.
For example, suppose SO is an ASP.NET Web Application project. The project code compiles down to the single .DLL in the BIN folder. Now, there are constantly users on SO, so what would happen to users' actions/sessions if you would use the Visual Studio .NET "Publish" feature (or just FTP everything again manually) while they are using the site?
Would creating an ASP.NET Web Site, instead, alleviate any problems that may or may not exist with the scenario above? I am beginning to develop a web site as a user-driven Web Application, and I want to make sure that my inexperience with this would not potentially annoy the [potentially] many users that I [want to] have 24/7.
EDIT: Sorry, I should have put this in a more exact context. Assume that this site is being hosted by a web hosting service with monthly fees. I won't be managing the server itself, just what the web host allows as a user of their services.
I create two Web sites in IIS. One is the production Web site, and the other is a static Web site with an HttpHandler that sends all requests to a single static "We're updating" HTML page served with an HTTP 503 Service Unavailable. Typically the update Web site is turned off. When it's time to update, we stop the production Web site, start the update Web site, and now we can fiddle with the production Web site all we want without worrying about DLLs being locked or worker processes needing to be spun down.
I started doing this because
App_Offline.htm really does not work well in Web Gardens, which we use.
App_Offline.htm serves its page as 404, which is bad if you're down for a meaningful period of time.
We can start the upgraded production Web site with modified settings (only listening on localhost), where we can do a last-minute acceptance/verification that everything is working before we flip the switch, turning off the update Web site and re-enabling the production Web site.
Things this does not solve include
Any maintenance that requires a restart of the server--you still have downtime where no page is served.
Any maintenance that diddles with the .NET runtime, like upgrading to the latest service pack.
Other approaches I've seen include
Having two servers. Send all load balancing requests to one server, upgrade the other one; then rinse and repeat. Most of us don't have this luxury.
Creating multiple bin directories, like bin-1.0.0.0 and bin-1.1.0.0 and telling ASP.NET which bin directory to use in the web.config file. (One advantage of this is that reverting to a previous binary is just editing a config file. A disadvantage is that it's harder to revert resources that don't end up in your binaries, like templates and images and such.) I don't remember how this actually worked--I think the application did some late assembly loading in its Global.asax based on its own web.config section (since you touched the web.config, the app had restarted, so it was okay).
If you find a better way, let me know!
Changing to the asp.net web site model won't have any effect, as the recycle will also happen, some of changes that trigger it for sure: web.config, global.asax, app_code.
After the recycle, user will still be logged in because asp.net will just validate the syntax. That is given you use a fixed machine key, otherwise it will change on each recycle. This is something you want to do anyway as other stuff can break if the key change across requests i.e. viewstate validation, embedded resources (decryption of the url fails).
If you can put the session out of process, like in sql server, you will avoid loosing the session. If you can't, your code will have to consider that. There are plenty of scenarios where you can avoid using session, and others were you can wrap it and re-retrieve the info if the session was cleaned. This should leave you with a handful specific cases that you know can give trouble to the users, so for those you do some of the suggestions others have already made.
One solution could be to deploy your application into a load balanced environment (web farm).
When deploying a new version you would use the load balancer to redirect requests to the server you are not deploying to.
App_offline.htm is great solution for this I think.
in SO we see application currently unavailable page when a deployment begins.
I am not sure how SO handles it.. But we usually put a holding page. So what ever the user has done (adding question or answering questions) does not get updated. As soon as he updates something he will see a holding page asking him to try after sometime.
And if I am the user I usually press the back button to make sure what I entered is saved in the browser history so that I can post later.
Some site use use are in clustered environment so I take one server offline and inform the load balancer that she will not be available and once I make sure that the new version is working fine I make it live.. I do the same thing for the next server.
Do we have any other option?
It is not a technical solution, but set up a scheduled maintenance window. You can annoucement in advance giving your user base fair warning that there is a possiblity that the application will not be available during that time frame.

Resources