Question: Why is the Session occasionally lost on some machines?
Answer: It may be related to the machine environment, such as firewall or anti-virus software. Try turning off the firewall.
Q: Why is the Session_End method not fired when Session.Abandon is called?
Answer: First of all, the Session_End method only supports InProc (in-process) type Session. Secondly, to activate the Session_End method, the Session must exist (that is, the Session has been used in the system), and at least one request must be completed (this method will be called in this request).
Q: Why is my Session often lost when I use it in InProc mode?
Answer: This problem is usually caused by the application being recycled, because when using the in-process Session, the Session is saved in the aspnet_wp process. When the process is recycled, the Session will naturally be gone. To determine whether the process has been recycled, you can use Check your system's Event Viewer for information.
For specific information, please refer to:
Session variables are lost intermittently in ASP.NET applications
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316148
There was also a bug in 1.0 that would cause the worker process to be recycled and restarted. This bug has been fixed in 1.1 and sp2.
For detailed information about this bug, please refer to:
ASP.NET Worker Process (Aspnet_wp.exe) Is Recycled Unexpectedly.
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q321792
Question: Why is the ID of the new Session the same as the original one after the Session times out or is Abandoned?
Answer: Because the SessionID is saved in the instance of the client browser, when the Session times out and the Session is re-established on the server, the Session ID passed by the browser will be used. Therefore, after the Session times out, the Session ID will not change after it is re-established.
Q: Why is the SessionID different for each request?
Answer: This problem may be caused by not saving any information in the Session, that is, the Session is not used anywhere in the program. After the information is saved in the Session, the SessionID will always be related to the browser, and the SessionID will not change at this time.
Q: Can Session be shared between ASP and ASP.NET?
Answer: Yes. But this is a relatively complicated process. Microsoft provides an official solution. Please refer to: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/ConvertToASPNET .aspQ
: What types of objects can be saved in Session?
Answer: This depends on the Session mode used. When using an in-process (InProc) Session, any object can be easily saved. If you use a non-InProc mode, you can only save objects that can be serialized and deserialized. If the object saved at this time does not support serialization, it cannot be saved in the Session of this mode (non-InProc).
Q: Why can't I use the Response.Redirect and Server.Transfer methods in Session_End to jump to a page?
Answer: Session_End is an event processing function fired inside the server. It is based on a timer inside the server. When the event is fired, there is no relevant HttpRequest object on the server, so the Response.Redirect and Server.Transfer methods cannot be used at this time.
Q: Can I get the HttpContext object in Session_End?
Answer: No, because this event is not associated with any request (Request) and has no request-based context.
Question: How to use Session in Web Service?
Answer: In order to use Session in Web Service, the caller of Web Service needs to do some extra work, and the Cookie used when calling Web Service must be saved and stored. For details, please refer to the MSDN documentation for the HttpWebClientProtocol.CookieContainer property. However, if you use a proxy server to access the Web Service due to framework limitations, the two cannot share the Session.
Question: Why can't I use Session when customizing my own HttpHandler?
Answer: When implementing your own HttpHandler, if you want to use Session, you must implement one of the following two mark interfaces: IRequiresSessionState and IReadOnlySessionState. These interfaces do not have any methods that need to be implemented. They are just a mark interface and the method of using the INamingContainer interface.
Q: When I use webfarm, why is the Session lost when I redirect to another web server?
Answer: For detailed information, please refer to:
PRB: Session State Is Lost in Web Farm If You Use SqlServer or StateServer Session Mode
http://support.microsoft.com/default.aspx?scid=kb;en-us;325056Q
: Why is my Session invalid in the Application_OnAcquireRequestState method?
Answer: Session will only be valid after the HttpApplication.AcquireRequestState event is called.
For detailed information please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconhandlingpublicevents.asp
Q: How do I direct from an HTTP page to HTTPS if cookieless is used?
Answer: Please try the following methods:
String originalUrl = "/fxtest3/sub/foo2.aspx";
String modifiedUrl = " https://localhost " + Response.ApplyAppPathModifier(originalUrl);
Response.Redirect(modifiedUrl);
Q: Session is valid in those events in global.asax?
Answer: Session is only valid after the AcquireRequestState event, and events after this event can use Session.
Q: How to get all objects saved in the current Session?
Answer: It can be obtained by traversing all Session.Keys. The code is as follows:
ArrayList sessionCollection = new ArrayList();
foreach (string strKey in Session.Keys){
sessionCollection.Add(Session[strKey]);
}
Q: Is it possible to share Sessions in different applications?
Answer: It cannot be shared directly. You can refer to how to share Session between ASP and ASP.NET.
Q: What is the difference between Session.Abandon and Session.Clear?
Answer: The main difference is that when using Session.Abandon, the Session_End method (in InProc mode) is called. The Session_Start method will be fired when the next request comes. Session.Clear only clears all data in the Session and does not terminate the Session, so those methods will not be called.
Question: In order to access the status value of Session sequentially, does Session provide a locking mechanism?
Answer: Session implements the Reader/Writer lock mechanism:
When the page has writable capabilities for the Session (that is, the page has the <%@ Page EnableSessionState="True" %> tag), the Session of the page holds a write lock until the request is completed.
When the page has read-only functionality for the Session (that is, the page has the <%@ Page EnableSessionState="ReadOnly" %> tag), the Session that requested completion of the page holds a read lock.
A read lock will block a write lock; a read lock will not block a read lock; a write lock will block all read and write locks. This is why when the same page in two frames writes to the same Session, one of them has to wait for the other (the slightly faster one) to complete before starting to write.
Q: What does Session smooth timeout mean?
Answer: Session smooth timeout means that as long as your page accesses (uses) the Session, the timeout will be refreshed (can be understood as retiming), that is, the timeout will be recalculated starting from the page request. However, the page cannot disable the Session. It will automatically access the Session of the current page and refresh the timeout.
Question: Why is Session invalid in the event handling function in global.asax?
Answer: It depends on which event handling function the Session is used in. Session is only valid after the AcquireRequestState event. All event handling functions after this event can use Session, but not those before it.
Question: When I write a component that depends on the current application's Session, why can't I directly use Session["Key"] to get its value?
Answer: Session["Key"] is actually this.Session["Key"], which is provided as a property of Page, so you cannot use this property directly in your component. You can use Session in the following ways:
HttpContext.Current.Session["Key"] = "My Seesion Value";
Q: When I use InProc mode to save the Session, where is the Session saved at this time?
Answer: Different IISs have different processing methods.
When using IIS5, the Session is saved in the process space of aspnet_wp.exe.
When using IIS6, all applications share the application pool by default, and the Session is saved in the process space of w3wp.exe.
Q: Is the Session timeout setting in minutes or seconds?
Answer: It is minutes, the default is 20 minutes.
Q: Will my Session be saved when an error occurs on the page? I need to handle some cleanup in Session_End, but it fails, why?
Answer: Session_End will only be executed when the Session is running in InProc mode. The account used by Session_End is the account running the aspnet_wp worker process (this can be set in machine.config). Therefore, if you use integrated security to connect to SQL in the Session_End method, it will open the link using the account of the aspnet_wp process, and success or failure at this time depends on your SQL security settings.
Question: Why do I lose the session when I redirect when I set cookieless to true?
Answer: When using cookieless, you must use relative paths to replace absolute paths in the program. If you use absolute paths, ASP.NET will not be able to save the SessionID in the URL.
For example: replace myDirmySubdirdefault.aspx with ..default.aspx.
Question: How to store SortedList in Session or Cache?
Answer: Please refer to the following methods:
SortedList x = new SortedList();
x.Add("Key1", "ValueA");
x.Add("Key2", "ValueB");
Save to Session:
Session["SortedList1"] = x;
Use the following method to obtain it:
SortedList y = (SortedList) Session["SortedList1"];
The same goes for Chahe.
Q: Why do I get the error message "Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive"?
Answer: This problem may occur after installing Window Sharepoint Server (WSS) on a machine that has installed Microsoft Visual Studio .NET development environment.
WSS ISAPI filter will handle all requests. When you browse an ASP.NET application through a virtual directory, the ISAPI filter does not assign URLs to the folder directory.
The solution is: Do not use Session on the machine where WSS is installed.
For detailed information please refer to:
Session state cannot be used in ASP.NET with Windows SharePoint Services
http://support.microsoft.com/default.aspx?scid=kb;en-us;837376Q
: How to delete Session variables?
Answer: If you want to delete the Session variable, you can use the HttpSessionState.Remove() method.
Q: Is there a way to know how much memory an application's Session takes up while it's running?
Answer: No. At present, this value cannot be verified, at least I have not seen any information on this yet. However, a value can be roughly estimated through the performance monitor and program code.
Question: When there is a frameset in the page, it is found that the SessionID of the page displayed in each frame is different in the first request. Why?
Answer: The reason is that your frameset is placed on an html page rather than an ASPX page.
Under normal circumstances, if the frameset is an aspx page, when you request the page, it first sends the request to the web server, and the SessionID has been obtained. Then the browser will request other pages in the Frame respectively, so that the SessionID of all pages It's the same, it's the SessionID of the FrameSet page.
However, if you use an Html page to make a FrameSet page, the first request will be the HTML page. When the page is returned from the server, no Session is generated. Then the browser will request the pages in the Frame, so these pages will generate their own SessionID, so in this case this problem arises. When you refresh the page, the SessionID will be the same, and it will be the SessionID of the last requested page.
Q: Is it possible to save Sessions of different applications on different databases on the same SQL Server server.
Answer: Yes, please refer to:
FIX: Using one SQL database for all applications for SQL Server session state may cause a bottleneck
http://support.microsoft.com/default.aspx?scid=kb;en-us;836680Q
: Can I get valid HttpSessionState and HttpContext objects in Session_End?
Answer: You can get the HttpSessionState object in this method, and you can access it directly using Session. However, the HttpContext object cannot be obtained because the event is not associated with any request, so there is no context object.
Q: When using Session in SQL Server mode, why does my Session not expire?
Answer: In SqlServer mode, Session expiration is completed through the registration of SQL Agent. Please check whether your SQL Agent is running?
Q: After I set EnableSessionState to "ReadOnly", I can still modify the Session value in InProc mode. Why is this?
Answer: Even if EnableSessionState is marked ReadOnly, users can still edit the Session in InProc mode. The only difference is that the Session will not be locked during the request.
Q: How can I avoid specifying a password when linking SQL?
Answer: Use a trusted link or use an encrypted link string. For more information on this please refer to:
How To Use the ASP.NET Utility to Encrypt Credentials and Session State Connection Strings
http://support.microsoft.com/default.aspx?scid=kb;en-us;329290Q
: How should I use Session in my own class?
Answer: You can use HttpContext.Current.Session. The specific method is as follows:
HttpContext.Current.Session["SessionKey"] = "SessionValue";
Similarly you can use the Application object in this way.
Q: Why is my request hung after switching to SQL Server mode?
Answer: Check whether all objects saved in the Session can be saved in SQL Server mode, that is, these objects must support serialization.
Question: What will be the impact when the Session is set to cookieless?
Answer: When cookieless is set to true, there are mainly the following constraints:
1. Absolute links cannot be used in the page
2. In addition to switching between HTTP and HTTPS, some other steps need to be completed in the application.
If you send a link to another person, the URL at this time will contain Session ID information, so the two people will share a Session.
Q: Can the Session be saved in the database?
Answer: Of course, for details, please refer to: http://support.microsoft.com/default.aspx?scid=kb;en-us;311209
----------------- -------------------------------------------------- -------------------------------------------------- -
Q: Why is my Session often lost when I use it in InProc mode?
An additional situation: If you use an Access database, in order to prevent the database from being downloaded, some people may think of putting the database file in the bin directory so that the database cannot be downloaded. However, if it is in InProc mode, this will also cause Session lost. Because when accessing the application, data will be written to the database frequently, which will lead to changes in the database files placed under the bin directory, and modifications to the bin directory will cause the session to be lost.
To solve this problem, you can change the storage path of the database file, or use StateServer or SqlServer mode.
For more information, please refer to:
PRB: Session Data Is Lost When You Use ASP.NET InProc Session State Mode
http://support.microsoft.com/default.aspx?scid=kb;en-us;324772
-------------------------------------------------- -----------------------
Multi-project shared session in Asp.net selected from dnyz's Blog
http://dev.csdn.net/article/21/21714.shtm
1. Create a blank solution blank solution, such as: d:MyProjectMyProject.sln
2. Create a Web Application root directory d:MyProjectWebMis under d:MyProject and set it to the virtual directory of http://localhost/WebMis
3. Create new directories according to modules in the WebMis directory, such as: d:MyProjectWebMisLogin and d:MyProjectWebMisCheckOut
4. Create a new web application based on the module in VS.net, such as: http://localhost/WebMis/Login and http://localhost/WebMis/CheckOut
5. After creation, the Login and CheckOut directories are automatically set as virtual directories.
6. Add project references for Login and CheckOut in the WebMis project
7. Delete the virtual directories of Login and CheckOut in IIS Manager
8. Delete the global.asax of each project (remove the root project)
9. Remove the following code in the project's web.config (remove the root project):
<authentication mode="Windows" />
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20" />
Or delete web.config (if you don’t need to configure it in each directory)
10. After compilation, it can be run.