Using session Frequently Asked Questions in. net

Source: Internet
Author: User

From http://www.itpub.net/866780.html

"

Q: Why is the session occasionally lost on some machines?
A: It may be related to the machine environment, such as firewall or anti-virus software. Try to disable the firewall.

Q: Why didn't the session_end method be activated when session. Abandon is called?
A: The session_end method only supports sessions of the inproc (in-process) type. Second, to stimulate the session_end method, there must be a session (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 session frequently lost in inproc mode?
A: This problem is usually caused by applications.ProgramThis is because when an in-process session is used, the session is stored in the aspnet_wp process. When this process is recycled, the session will naturally disappear, to determine whether the process is recycled, you can obtain information by viewing the System Event Viewer.
For more information, see:
Session variables are lost intermittently in ASP. NET Applications
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; q2017148
At 1.0, a bug also causes the worker process to be recycled and restarted. The bug has been fixed in 1.1 and SP2.
For more information about this bug, see:
ASP. NET Worker Process (aspnet_wp.exe) is recycled unexpectedly.
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; q321792

Q: Why is the new session ID the same as the original one after the session times out or abandoned?
A: Because sessionid is saved in the client browser instance, when the session times out and the server re-creates the session, the session ID sent from the browser will be used. When the session times out, then, the sessionid remains unchanged.

Q: Why are the sessionids of each request different?
A: 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 session information is saved, the sessionid will always be related to the browser. At this time, the sessionid will not change.

Q: Can sessions be shared between ASP and ASP. NET?
A: Yes. But this is a relatively complex process, Microsoft provides the official solution, please refer to: http://msdn.microsoft.com/librar... converttoaspnet. asp

Q: What types of objects can be stored in the session?
A: This depends on the session mode. When using an inproc session, you can easily save any object. If you use the non-inproc mode, you can only save the objects that can be serialized and deserialized. If the stored objects do not support serialization, they cannot be saved to this mode (non-inproc).

Q: Why can't I use the response. Redirect and server. transfer methods to jump to the page in session_end?
A: session_end is an event processing function that is triggered inside the server. It is based on the Internal timer of a server. When the event is triggered, there is no related httprequest object on the server. Therefore, the response. Redirect and server. transfer methods cannot be used at this time.

Q: Can I obtain the httpcontext object in session_end?
A: No, because this event is not associated with any request and is not based on the request context.

Q: How do I use session in Web service?
A: To use a session in a web service, you need to do some additional work on the web service caller. You must save and store the cookies used to call the web service. For more information, see the httpwebclientprotocol. cookiecontainer attribute in the msdn document. However, if you use a proxy server to access the Web service due to framework restrictions, the two cannot share sessions.

Q: Why can't I use a session when customizing my httphandler?
A: When implementing your own httphandler, if you want to use the session, you must implement one of the following two Mark interfaces: irequiressessionstate and ireadonlysessionstate. These interfaces do not have any implementation methods, the method for marking an interface is the same as that for using the inamingcontainer interface.

Q: When I use webfarm, why is the session lost when I redirect to another Web server?
A: For more information, see:
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; 325056

Q: Why is my session invalid in the application_onacquirerequeststate method?
A: The session is valid only when the httpapplication. acquirerequeststate event is called.
For more information, see:
Http://msdn.microsoft.com/librar... ingpublicevents. asp

Q: If cookieless is used, how can I redirect an HTTP page to HTTPS?
A: try the following method:
String originalurl = "/fxtest3/SUB/foo2.aspx ";
String modifiedurl = "https: // localhost" + response. applyapppathmodifier (originalurl );
Response. Redirect (modifiedurl );

Q: Are sessions valid in global. asax events?
A: The session is valid only after the acquirerequeststate event. All events after the event can use the session.

Q: How do I obtain all objects saved in the current session?
A: You can retrieve all session. Keys.CodeAs follows:
Arraylist sessioncollection = new arraylist ();
Foreach (string strkey in session. Keys ){
Sessioncollection. Add (session [strkey]);
}

Q: Can I share sessions in different applications?
A: You cannot share data directly. You can refer to how to share a session between ASP and ASP. NET.

Q: What is the difference between session. Abandon and session. Clear?
A: The main difference is that when session. Abandon is used, the session_end method is called (in inproc mode ). The session_start method is triggered when the next request arrives. Session. Clear only clears all data in the session and does not stop the session. Therefore, it does not call those methods.

Q: Does the session provide a locking mechanism to allow sequential access to session Status values?
A: The session implements the reader/writer lock mechanism:
When the page has a writable function for the session (that is, the page has a <% @ page enablesessionstate = "true" %> flag), the session of the page holds a write lock until the request completes.
When the page has the read-only function for the session (that is, the page is marked with <% @ page enablesessionstate = "readonly" %>), the session of the page is read locked.
A read lock will block a write lock; A read lock will not block a read lock; A write lock will block all read/write locks. This is why when the same page in the two frameworks writes the same session, one of them starts to write after the other (the one that is a little faster) completes.

Q: What does session smooth timeout mean?
A: The session smooth timeout means that as long as you access (use) The session, the timeout will be refreshed (which can be understood as re-timing), that is, starting from the page request, the timeout value is recalculated. However, the session cannot be disabled on this page. It will automatically access the session on the current page and refresh the timeout time.

Q: Why is the session invalid in the event handler function in global. asax?
A: It depends on the event processing function in which the session is used. The session is valid only after the acquirerequeststate event. All event processing functions after the event can use the session, but not the previous one.

Q: Why can't I directly use session ["key"] to obtain the value of a session that depends on the current application?
A: session ["key"] is actually this. session ["key"], which is provided as an attribute of page, so you cannot directly use this attribute 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 a session, where is the session stored?
A: Different IIS processing methods are different,
When iis5's session is used, the session is stored in the process space of aspnet_wp.exe.
When iis6is used, all application sharing application pools are in bad condition, and sessionis stored in the w3wp.exe process space.

Q: Is the Session Timeout set in minutes or seconds?
A: It is minute. The default value is 20 minutes.

Q: Will my session be saved when an error occurs on the page? I need to handle some cleanup work in session_end, but it fails. Why?
A: session_end is executed only when the session runs in inproc mode. Session_end is the account that runs the aspnet_wp workflow (this can be set in machine. config ). Therefore, if you use the Integrated Security link to SQL in the session_end method, it will use the account of the aspnet_wp process to open the link. At this time, success and otherwise depend on your SQL security settings.

Q: Why is the session lost when I set cookieless to true?
A: When cookieless is used, you must replace the absolute path in the program with the relative path. If ASP. NET is used, sessionid cannot be saved in the URL.
For example, replace \ mydir \ mysubdir \ default. aspx with .. \ default. aspx.

Q: How can I store sortedlist in the session or cache?
A: refer to the following method:
Sortedlist x = new sortedlist ();
X. Add ("key1", "valuea ";
X. Add ("key2", "valueb ";
Save to session:
Session ["sortedlist1"] = X;
You can use the following method to obtain it:
Sortedlist y = (sortedlist) session ["sortedlist1"];
The same applies to 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 ve "?
A: This problem may occur after Windows Sharepoint Server (WSS) is installed on a machine that has the Microsoft Visual Studio. NET development environment installed.
The wss isapi filter processes all requests. When you browse an ASP. NET application through a virtual directory, the ISAPI filter does not assign a URL to the folder directory.
Solution: Do not use Session on the machine where WSS is installed.
For more information, see:
Session state cannot be used in ASP. NET with Windows SharePoint Services
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; 837376

Q: How do I delete session variables?
A: You can use the httpsessionstate. Remove () method to delete the session variable.

Q: Is there a way to know how much memory the application session occupies during running?
A: No. At present, this value cannot be verified. At least I have not yet seen this information. However, you can estimate a value through the performance monitor and program code.

Q: If frameset is specified on the page, the sessionid of the page displayed in each frame is different in the first request. Why?
A: The reason is that your frameset is placed on an HTM page instead of An ASPX page.
In general, if the frameset Is An ASPX page, When you request a page, it first sends the request to the Web server. At this time, the sessionid is obtained, then the browser requests other pages in the frame respectively, so that the sessionids of all pages are the same, that is, the sessionid of the frameset page.
However, if you use an HTML page as a frameset page, the first request will be an 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 this problem will occur in this case. When you refresh the page, sessionid will be the same, and it is the sessionid of the last request page.

Q: Can sessions of different applications be stored in different databases on the same SQL Server server.
A: Yes. See:
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; 836680

Q: Can I obtain valid httpsessionstate and httpcontext objects in session_end?
A: You can obtain the httpsessionstate object in this method. You can directly use session to access the object. However, the httpcontext object cannot be obtained because the event is not associated with any request, so there is no context object.

Q: Why does my session not expire when I use session in sqlserver mode?
A: In sqlserver mode, session expiration is completed through SQL Agent Registration. 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?
A: even if the enablesessionstate is set to readonly, You can edit the session in inproc mode. The only difference is that the session will not be locked during the request process.

Q: How can I avoid specifying a password when connecting SQL statements?
A: Use a trusted link or encrypted link string. For more information, see:
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; 329290

Q: How can I use session in my own class?
A: You can use httpcontext. Current. session as follows:
Httpcontext. Current. session ["sessionkey"] = "sessionvalue ";
Similarly, you can use the application object in this way.

Q: Why is my request suspended after I switch to sqlserver mode?
A: Check whether all objects saved in the session are objects that can be saved in sqlserver mode. That is, these objects must support serialization.

Q: What will happen when the session is set to cookieless?
A: When cookieless is set to true, the following constraints are imposed:
1. You cannot use absolute links on the page.
2. Some other steps are required in addition to switching between HTTP and HTTPS in the application.
If you send a link to another user, the URL contains the session ID information, so the two users share a session.

Q: Can I save the session in the database?
A: Of course. For more information, see: http://support.microsoft.com/default.aspx? SCID = KB; en-US; 311209

"

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.