asp.net delete file session missing

  • 2020-05-10 18:01:15
  • OfStack

If you have ever modified the ASP.NET application (dll files), and modified the bin folder or Web.config files (add/delete/rename files, etc.) while the site is running, you may have noticed that this will cause a reboot in AppDomain. All session state will be lost and the site will launch again successfully, and any logged in users will be logged out (assuming you do not use persistent Cookie authentication). Of course, when we modify the web.config file and save it, forcing 1 AppDomain to restart, this is what we need.

We sometimes create and delete folders on the fly, and in ASP.NET 2.0, folder deletion will cause a reboot of one AppDomain, which can cause serious problems. For example, for a product on an e-commerce site, you might want to store in the catalog a picture of the product from its name ID, for example. productImages/ productImages/123/ ipod-nano. jpg even for id image recording. This helps avoid file name conflicts with other uploaded files and images. Of course, when you come to delete from the database product, you will naturally delete its corresponding image and folder containing it, but obviously not because of this AppDomain reboot issue. Because, we delete the empty folder left on our server (file deletion does not cause the application to restart).

The solution

Fortunately, we have the Reflection and HttpModules solution. First create an image.cs file...
 
using System.Reflection; 
using System.Web; 
namespace MyWebsite 
{ 
/// <summary> 
/// Stops the ASP.NET AppDomain being restarted (which clears 
/// Session state, Cache etc.) whenever a folder is deleted. 
/// </summary> 
public class StopAppDomainRestartOnFolderDeleteModule : IHttpModule 
{ 
public void Init(HttpApplication context) 
{ 
PropertyInfo p = typeof(HttpRuntime).GetProperty("FileChangesMonitor", 
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); 
object o = p.GetValue(null, null); 
FieldInfo f = o.GetType().GetField("_dirMonSubdirs", 
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase); 
object monitor = f.GetValue(o); 
MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", 
BindingFlags.Instance | BindingFlags.NonPublic); 
m.Invoke(monitor, new object[] { }); 
} 
public void Dispose() { } 
} 
} 

If you prefer to use Global.asax in Application_Start, place the Init () code in Application_Start. I believe that the Global.asax usage method is outdated, and that HttpModules can be used in response to the network (session start, session end, application lifecycle). The init method has the same effect on Global.asax as on Application_Start, and Dipose is similar to Application_End.

To work with the above code, we need to use the web.config file < httpModules > Put:

< add name="stopAppDomainRestartOnFolderDelete"
type="MyWebsite.StopAppDomainRestartOnFolderDeleteModule" / >
Note that "stopAppDomainRestartOnFolderDelete" is a custom arbitrary name,"MyWebsite" is the namespace in the.cs file above, usually the project name, and "StopAppDomainRestartOnFolderDeleteModule" is the class name in the.cs file above.

That's it. This will prevent the folder from deleting the AppDomain and restarting, but it will still restart when you modify the web.config and bin folders, which is exactly what we want.

But delete a few more files and you'll find that session still expires. Why is that? It's not clear yet... The following is the way to search on the Internet

in < system.web > Just configure session to be saved as stateserver

< sessionState mode="StateServer" stateNetworkTimeout="20"
stateConnectionString="tcpip=127.0.0.1:42424" / >
Parameter 1 will tell you what it means..

Related articles: