Yet another frustrating error. That when you delete a directory the session gets expired automatically.
This is a known issue and is actually a default behavior and is defined in following words.
It is the default nature if you are deleting a subdirectory within your application, your app domain will restart. This removes all session data.
How to overcome this issue?
Here are the 3 possible solutions for this issue.
Here is the explanation how to use the directory which is outside. I got this solution from the given link as well.
http://stackoverflow.com/questions/502743/asp-net-deleting-a-directory-results-in-application-restart
I am not stealing the credits for this solution. I got the idea of this from following article and then made my own function with little bit of modifications
This is a known issue and is actually a default behavior and is defined in following words.
It is the default nature if you are deleting a subdirectory within your application, your app domain will restart. This removes all session data.
How to overcome this issue?
Here are the 3 possible solutions for this issue.
Solution 1:
Make your directory outside application directory.Here is the explanation how to use the directory which is outside. I got this solution from the given link as well.
http://stackoverflow.com/questions/502743/asp-net-deleting-a-directory-results-in-application-restart
Solution 2:
Don't delete the directory but only delete files in directory and sub directories. This will at least release the memory. Here is my function to Delete all files (and not directories). Simply call it instead of the Directory.Delete Statement.VB
Private Sub DeleteFiles(pDirectoryPath As String)
For
Each mFile In
System.IO.Directory.GetFiles(pDirectoryPath)
System.IO.File.Delete(mFile)
Next
For
Each mDirectory In
System.IO.Directory.GetDirectories(pDirectoryPath)
DeleteFiles(mDirectory)
Next
End Sub
CSharp (C#)
private
void DeleteFiles(string pDirectoryPath)
{
foreach (object mFile_loopVariable in System.IO.Directory.GetFiles(pDirectoryPath)) {
mFile = mFile_loopVariable;
System.IO.File.Delete(mFile);
}
foreach (object mDirectory_loopVariable in
System.IO.Directory.GetDirectories(pDirectoryPath)) {
mDirectory =
mDirectory_loopVariable;
DeleteFiles(mDirectory);
}
}
Solution 3:
This will work for your local system and for dedicated/virtually dedicated hosting and for only those shared hosting which allow you to run your application/website in full trust mode.
You have to add following function in your code and then have to call it before you use the Directory.Delete method.VB
Private Sub FixAppDomainRestartWhenTouchingFiles()
Dim p As System.Reflection.PropertyInfo
= GetType(HttpRuntime).GetProperty("FileChangesMonitor", Reflection.BindingFlags.Public Or
Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
Dim o As Object =
p.GetValue(Nothing, Nothing)
Dim f As System.Reflection.FieldInfo
= o.GetType.GetField("_dirMonSubdirs",
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
Or Reflection.BindingFlags.IgnoreCase)
Dim monitor As Object = f.GetValue(o)
Dim m As System.Reflection.MethodInfo
= monitor.GetType.GetMethod("StopMonitoring",
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
m.Invoke(monitor, New Object() {})
End Sub
CSharp (C#)
private
void FixAppDomainRestartWhenTouchingFiles()
{
System.Reflection.PropertyInfo p = typeof(HttpRuntime).GetProperty("FileChangesMonitor",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Static);
object o
= p.GetValue(null, null);
System.Reflection.FieldInfo f =
o.GetType().GetField("_dirMonSubdirs",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.IgnoreCase);
object
monitor = f.GetValue(o);
System.Reflection.MethodInfo m =
monitor.GetType().GetMethod("StopMonitoring",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
m.Invoke(monitor, new object[]);
}
Again I won't try to steal the credits.
Here is the link from where I got this solution.
http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/12/disable-session-expiration-when-using-directory-delete.aspx
Here is the link from where I got this solution.
http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/12/disable-session-expiration-when-using-directory-delete.aspx
No comments:
Post a Comment