If you have ever modified the application (dll file), with modified the bin folder or file (add/delete/renamed files, etc.) and the site is running, you may have noticed that this will result in a restart in AppDomain. All session status will be lost and the website will be launched successfully again, and any logged in user will be logged out (assuming you do not use persistent cookie authentication). Of course, when we modify the file and save it, forcing an AppDomain to restart, that's what we need.
We sometimes create and delete folders dynamically, in 2.0, folder deletion will cause an AppDomain to restart, which will cause serious problems. For example, for an e-commerce website product, you might want to store a product in a directory from its name ID product, for example. /productImages/123/, even for the recording of the ID card image. This helps avoid conflicts with other uploaded files and image file names. Of course, when you come to delete the product from the database, you naturally have to delete its corresponding image and folder containing it, but obviously you can't because of this problem with AppDomain restart. Because, we delete the empty folders left in our server (file deletion will not cause the application to restart).
Solution
Fortunately, we have a solution for Reflection and HttpModules. First create a file like .cs...
using ;
using ;
namespace MyWebsite
{
/// <summary>
/// Stops the 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",
| | );
object o = (null, null);
FieldInfo f = ().GetField("_dirMonSubdirs",
| | );
object monitor = (o);
MethodInfo m = ().GetMethod("StopMonitoring",
| );
(monitor, new object[] { });
}
public void Dispose() { }
}
}
If you prefer to use the Application_Start file, place the Init() code in Application_Start. I believe that when the usage method is outdated, when using HttpModules, it can respond to the network (the session starts, ends of the session,). The init method has the same function as Application_Start, and Dipose is similar to Application_End.
To work with the above code, we need to put it in the <httpModules> section of the file:
<add name="stopAppDomainRestartOnFolderDelete"
type="" />
It should be noted that "stopAppDomainRestartOnFolderDelete" is a custom arbitrary name, and "MyWebsite" is the namespace in the above .cs file, generally the project name. "StopAppDomainRestartOnFolderDeleteModule" is the class name in the above .cs file.
That's it. This will prevent the folder from deleting AppDomain restarting, but will still restart when modifying and bin folders, which is exactly what we want.
But if you delete a few more files, you will find that the session will still expire. Why is this the case? Now that I haven't figured it out... So I searched online and found the following method
Just configure the save mode of the session to stateserver under <>
<sessionState mode="StateServer" stateNetworkTimeout="20"
stateConnectionString="tcpip=127.0.0.1:42424" />
You can tell what the parameters mean at a glance..
We sometimes create and delete folders dynamically, in 2.0, folder deletion will cause an AppDomain to restart, which will cause serious problems. For example, for an e-commerce website product, you might want to store a product in a directory from its name ID product, for example. /productImages/123/, even for the recording of the ID card image. This helps avoid conflicts with other uploaded files and image file names. Of course, when you come to delete the product from the database, you naturally have to delete its corresponding image and folder containing it, but obviously you can't because of this problem with AppDomain restart. Because, we delete the empty folders left in our server (file deletion will not cause the application to restart).
Solution
Fortunately, we have a solution for Reflection and HttpModules. First create a file like .cs...
Copy the codeThe code is as follows:
using ;
using ;
namespace MyWebsite
{
/// <summary>
/// Stops the 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",
| | );
object o = (null, null);
FieldInfo f = ().GetField("_dirMonSubdirs",
| | );
object monitor = (o);
MethodInfo m = ().GetMethod("StopMonitoring",
| );
(monitor, new object[] { });
}
public void Dispose() { }
}
}
If you prefer to use the Application_Start file, place the Init() code in Application_Start. I believe that when the usage method is outdated, when using HttpModules, it can respond to the network (the session starts, ends of the session,). The init method has the same function as Application_Start, and Dipose is similar to Application_End.
To work with the above code, we need to put it in the <httpModules> section of the file:
<add name="stopAppDomainRestartOnFolderDelete"
type="" />
It should be noted that "stopAppDomainRestartOnFolderDelete" is a custom arbitrary name, and "MyWebsite" is the namespace in the above .cs file, generally the project name. "StopAppDomainRestartOnFolderDeleteModule" is the class name in the above .cs file.
That's it. This will prevent the folder from deleting AppDomain restarting, but will still restart when modifying and bin folders, which is exactly what we want.
But if you delete a few more files, you will find that the session will still expire. Why is this the case? Now that I haven't figured it out... So I searched online and found the following method
Just configure the save mode of the session to stateserver under <>
<sessionState mode="StateServer" stateNetworkTimeout="20"
stateConnectionString="tcpip=127.0.0.1:42424" />
You can tell what the parameters mean at a glance..