ASP.NET Web Application Restarts When Deleting Directory - c#

I have some code in my logout routine that deletes some temporary session files when the user logs out or when the session expires. Deleting these folders causes my web app to restart. It does not error out or throws an exception it just restarts! Any ideas?

That's by design. There's a threshold to the number of files that can change outside the bin folder, and if they do, the app restarts.
If you change anything at all inside the bin folder, it also restarts.
You need to save the temporary files somewhere else. You could save them in the %TEMP% folder (you can use Path.GetTempPath() to get it), or create a folder for them specifically OUTSIDE of your web app virtual directory and save the files there.

Related

Accessing folders ouside of root IIS

I need to write files into a folder using c# that is outside of root folder on IIS 7.
I have made the folder and given IIS_IUSRS and the app pool users all rights on the folder but I always get UnauthorizedAccessException when I try to write to it.
Everything I googled says the solution is virtual directory, but I cant have the folder emptied every time I publish the web.
Is there a solution to this?
You also need to grant file access permissions to the NETWORK SERVICE account.

ASP.NET 4.5 -- Application pool restart when sub-directory is deleted in a virtual directory

Just to preface, before I decided to ask this question, I have searched up and down StackOverflow.
The problem I am having is w/r/t to deleting sub-directories in an ASP.NET web application and it causing an application pool restart.
The folder that we upload files into, as well as delete/create sub-directories in, is off the root of the application and setup as a virtual directory in IIS.
A zip can be uploaded the contains html and images. We show a preview of the html as well as the images.
The user can re-upload another zip. When this happens, we delete the previous temporary folder, and then create another temporary folder to hold the contents of the zip and render another preview.
It's during the deletion of the previous folder that we see an app restart. The snippet of the log output is:
2014-02-10 16:06:45,240 [16] global_asax - Application_End(): CONFIG change
HostingEnvironment initiated shutdown
HostingEnvironment caused shutdown: at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
We've had this setup from when our application used to run under ASP.NET 2.0 and this was the 'recommended' setup to avoid application pool restarts and creating/deleting directories.
We've recently upgraded our web application to ASP.NET 4.5 and we are seeing this behavior in our development environment.
Based on this post, http://connect.microsoft.com/VisualStudio/feedback/details/646824/asp-net-application-restart-ignore-folder-setting, which says that ASP.NET 4 has relaxed on the monitoring for directory changes when the folders are under the web root as well as this post, http://blogs.msdn.com/b/tess/archive/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles.aspx, which states that a change to the physical structure of a virtual directory can cause an application pool restart. I've moved the folders back under the web root but I am still seeing the behavior.
What's interesting is that if we comment out the piece of code that creates the preview (An iFrame that renders the html which will reference any images uploaded with the zip to its temporary location i.e.: domain.com/temp/2//images/header.gif. The deletion happens with no application restart.
Any help, or advice of where to look next would be greatly appreciated.

Prevent delete user generated content on visual studio 2010 publish

I have an application that stores images uploaded by user. Those images goes to content/images/upload. Problem is that if i published without the skip extra files on destination, all user uploaded content gets deleted. Im pretty sure im doing it wrongly, which would be the best approach for this situation ?
im worried about losing user generated content this way misconfiguring an option in publish.
One approach I've used is to move the physical path of the "upload" older outside the application folder structure. I'd use an IIS virtual directory to mount the folder to the application. For example, if the folder structure currently looks like this:
c:\inetpub\
wwwroot\
content\
images\
upload\
You might change it to this:
c:\inetpub
upload\ <-- must be manually created on the web server
wwwroot\
content\
images\
You'd remove the upload folder from the web application in Visual Studio. Log into the webserver and create the folder under inetpub (could go anywhere, really). Then go into IIS manager and create a virtual directory called upload that points to the c:\inetpub\upload folder.
The upload folder is no longer part of the Visual Studio project so the "publish" feature won't touch it.

Upload files from two different applications to same folder

Is it possible to upload files from different web applications in a same folder.
Application A corresponds to a register page, and application B administrator can track every person is register in application A.
The problem is that administrator of app B cannot see the documents uploaded in app A since they where uploaded in a different folder.
How can I save the uploaded files of app A into de folder of app B.
The usual way of doing this is to have a common folder outside of both applications that they can both see. Inside of IIS you set this up as a virtual directory in each application.
From the applications perspective, it looks like the folder is local to it's root path. However, it's physically stored elsewhere.
Steps
Go to the file system on your web server.
Create a directory somewhere to hold your uploads. Give your app pool rights to read and write files to that directory.
Open IIS Manager on the web server.
Navigate to Application A.
Right click on the site and select Add Virtual Directory
Enter an alias for it and set the path to the directory you created in step 2.
Do steps 4,5 and 6 for Application B.
From Application A, change your code to save your files to that directory. From Application B change the code to pull the files from that directory.

Deleting Directory at runtime

I just want to delete the Directory which is in the project folder.
That folder having all web sharing authority and permission.
The problem is arising when i am deleting the folder.
Folder is deleted from the serverpath(Virtual Directory).
But my problem is that when i complete my task and click on any control it will redirect me to the Login page with the return url as there is a secure authentication on the root directory is available
Application domain recycled when
Sub-Directories are deleted, that's why your session will lost and you are redirected to the login page.
For more details, check this article from MSDN Blog and read Why does an application domain recycle? from here
ASP.NET Case Study: Lost session variables and appdomain recycles
and also check this one Deleting ASP.NET 2.0 Application Sub-Directories Shuts Down the AppDomain
As noted in another answer, this is because the web app is restarted whenever you delete a folder inside the web app's directory structure (i.e. below the web app's root directory).
The only solution I found for this problem is to move the data directories (which you create/delete/modify) outside the web app's root directory / virtual directory.
Then we create a link (junction) in the file system so that the directory appears to be inside the virtual directory. This prevents ASP.NET from monitoring the data directory for delete operations.
Example:
Our web site (virtual directory) is located at C:\projectX\website
the data directory (where we create/delete files and folders) is located at C:\projectX\data
then we create a link which makes the data folder available as C:\projectX\website\data
The link is created using the program Linkd.exe (available in the windows resource kit), with the following command:
linkd c:\projectX\website\data c:\projectX\data
Now C:\projectX\website\data is a link/junction which points to the real data directory. Inside your web app, you can continue working as if the data directory were a physical directory below the web app's root directory.
E.g. in your web site you can access the data folder using this code:
Server.MapPath("~/data")
And you can also used the windows file explorer and browse to C:\projectX\website\data. It appears just like a real directory.
As you can see, you can continue to use the linked data folder as if it were a normal folder inside the web app's directory. The only difference is that ASP.NET will not track the directory for delete operations and will therefore not restart the application. This means, you can now create/delete/modify folders and files inside the ~/data directory as you wish, without having the web app restarted.
Can you replace folder from the project root into the App_Data folder?

Categories