Asp.net File Deletion session loss

Source: Internet
Author: User
Tags custom name

If you have modified ASP.. NET application (dll file), and modified the bin folder or Web. config File (Add/delete/rename file, etc.), and the web site is running, you may have noticed that this will cause the AppDomain to restart. All session statuses will be lost and the website will be successfully started again, and any login user will be exited (assuming you do not use persistent Cookie authentication ). Of course, when we modify the web. config file and save it to force an AppDomain to restart, this is what we need.

Sometimes we dynamically create and delete folders. in ASP. NET 2.0, deleting folders will cause an AppDomain to restart, which will cause serious problems. For example, for an e-commerce website product, you may want to store images of the product in the directory from its name and ID, for example. /ProductImages/123/ipod-nano.jpg, or even a record for the ID card image. This helps avoid conflicts with other uploaded files and image file names. Of course, when you come to delete a database product, you naturally need to delete the corresponding image and the folder containing it, but obviously this AppDomain cannot be restarted. Because, we delete the empty folder that is left on our server (deleting the file will not cause the application to restart ).

Solution

Fortunately, we have a Reflection and HttpModules solution. First create a file like. cs...
Copy codeThe Code is as follows:
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 the Global. asax file in Application_Start, place the Init () code in Application_Start. I believe that the use of Global. asax is outdated and that HttpModules can respond to the network (THE SESSION OF THE APPLICATION lifecycle starts at the end of the session ,). The init method works the same with Application_Start in Global. asax. The Dipose method is similar to Application_End.

To make the above Code work, you need to put it in the
<Add name = "stopAppDomainRestartOnFolderDelete"
Type = "MyWebsite. StopAppDomainRestartOnFolderDeleteModule"/>
It must be noted that "stopAppDomainRestartOnFolderDelete" is a custom name, and "MyWebsite" is the preceding. the namespace in the cs file, usually the project name. "StopAppDomainRestartOnFolderDeleteModule" is the preceding. class name in the cs file.

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

However, if you delete several more files, you will find that the session will still expire. Why? I haven't figured it out yet. So the following method is available for searching on the Internet.

Configure the session storage mode as stateserver under <system. web>.

<SessionState mode = "StateServer" stateNetworkTimeout = "20"
StateConnectionString = "tcpip = Wagner. 0.0.1: 42424"/>
You can see what the parameter means ..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.