asp.net delete file session missing

Source: Internet
Author: User
Tags add object config net reflection custom name
If you have ever modified a asp.net application (DLL file) with a modified bin folder or Web.config file (Add/remove/rename files, etc.) while the site is running, you may have noticed that this will lead to a reboot in AppDomain. All session state will be lost and the site will be restarted successfully, and any logged-in user would be evicted (assuming you do not use persistent cookie authentication). Of course, when we modify the Web.config file and save it, forcing a AppDomain reboot, this is what we need.

We sometimes dynamically create and delete folders, in asp.net 2.0, folder deletion will cause a AppDomain reboot, which will cause serious problems. For example, for a product of an ecommerce site, you may want to store a product in the catalog from its name ID of the product's picture, for example. /productimages/123/ipod-nano.jpg, even for the identity card image of the record. This helps avoid conflicts with other uploaded file and image file names. Of course, when you come to delete a product from a database, you naturally want to delete its corresponding image and contain its folder, but obviously not because of this AppDomain reboot problem. Because, we delete the empty folder left in our server (file deletion does not cause application reboot).

Solution

Luckily, we have a solution for reflection and httpmodules. 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 Global.asax usage method is obsolete and can be used in response to the network (the session at the application lifecycle begins, at the end of the session) using HttpModules. The Init method is the same as the Application_Start function in Global.asax, dipose similar to Application_End.

We want to work with the above code, and we need to put in the Web.config file
<add name= "Stopappdomainrestartonfolderdelete"
Type= "Mywebsite.stopappdomainrestartonfolderdeletemodule"/>
It should be noted that "Stopappdomainrestartonfolderdelete" is a custom name, "MyWebsite" is a namespace in the above. cs file, typically the project name. " Stopappdomainrestartonfolderdeletemodule "is the class name in the above. cs file.

That's it. This will prevent the folder from being deleted AppDomain reboot, but will still reboot when modifying the web.config and Bin folders, which is exactly what we want.

But the deletion of several files will find that the session will still expire, why is this so? It's not clear yet ... So the search on the Internet has the following way

Configure the session to be saved under <system.web> as StateServer.

<sessionstate mode= "StateServer" statenetworktimeout= "20"
stateconnectionstring= "tcpip=127.0.0.1:42424"/>
Parameters to see what the meaning of the.

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.