Out-of-process session
Introduction
Http is a stateless connection protocol, while http is used by the World Wide Web. Therefore, our web pages are stateless connections at the root, namely: when a user sends a request, the server processes the request, and then returns the result to the user, there is no connection between them. However, this is not the case in our actual development website because the development platform we use makes up for this problem.
When dealing with http stateless connections, the. net platform uses cookies, sessions, applications, viewstate, and hidden domains. This blog focuses on sessions.
Session is a session-level object. When our program uses it, the server program creates a space for each user who uses the page, so as to implement the existing State connection, from this point of view, we can draw a shortcoming. If many users access this page, the server will not open up a lot of data space, which is a waste of memory resources.
At this level, we can barely say that we can use sessions because there are fewer memory resources, but the program can still run. However, if we talk about IIS stability, that's the end.
The IIS server provided by Microsoft is very powerful and stable. Its stable maintenance is like this: when blocking cannot process requests, it will automatically restart. In addition, we can change the corresponding settings for application pool recycle. This is very scary. You want to restart IIS or the application pool is recycled, so there is no more data in the Session of our program? Think about how a user who just boarded the website will respond?
The above Session is an in-process Session. Of course, if there is an in-process Session, there will be an out-of-process Session. From the shortcomings of asp.net sessions mentioned above, in other words, the topic of this blog cannot be completely introduced, because for small websites, the shortcomings of the Session in the above process are not a problem, and it is estimated that it will not happen frequently, A small number of users! So what is the content of another question?
Another reason that leads to Session outside the process is the cluster web program. After developing a website, we need to deploy the same program on different computers to implement the cluster web program, what is the effect of running web Cluster programs?
For example, we deploy the same web program on three computers, A, B, and C. When the same user accesses this website, the first access is the program of computer A. The second access may be the program of computer B. The third access may be the program on computer C, this is the effect achieved by web Cluster programs. Of course, to achieve this effect, we must first consider the sharing of State data, which is obvious, if our Session is in the process at this time, I need to log on to the program that accesses server A. when accessing the program of server B, I need to log on again. Therefore, our out-of-process Session will also come out.
Overview of the out-of-process Session service provided by Microsoft
Microsoft provides the process-based Session service. When using the service, you need to enable it: Start-computer-management-Asp.net status service, or enter services. msc in the Command window. In addition, we need to modify the registry so that it supports remote connection. Open the registry and find HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ aspnet_state \ Parameters \ AllowRemoteConnection, and change it to 1.
Cache in memory
Add the following nodes to the configuration file:
<SessionStatemode = "StateServer" stateConnectionString = "tcpip = localhost: 42424" timeout = "30" cookieless = "false"> </sessionState>
Then, simply use the Session as usual.
Cache in Database
Create a database and table for storing Session data. You can use a business database or create a table independently, microsoft has provided us with the corresponding SQL, and we can go to the corresponding version. find the SQL statement in the. net framework and run the following command in SqlServer: aspnet_regsql.exe-S localhost \ Server-UuserName-P password-ssadd-sstype c-d SqlSessionTest, localhost \ Server: a specific database Server; SqlSessionTest: Database Name; two more tables will be generated after the command is executed
Add the following nodes to the configuration file:
<SessionState mode = "SQLServer" allowCustomSqlDatabase = "true" sqlConnectionString = "Data Source = localhost \ Server; InitialCatalog = SqlSessionTest; uid = userName; pwd = password"> </sessionState>
After completing the preceding operations, you can use the Session normally.
Note:
The Socket used for the transfer between the program and the non-process Session. Therefore, when it comes to saving the object of a custom class on the Session, we need to add a serialization tag on the class: [Serializable];
The non-process Session mechanism provided by Microsoft is not efficient. Therefore, it is generally not cached in the memory or in the database. To solve this problem, we can use Memcached to simulate the Session.
Memcached simulated Session Service
Session is a mechanism provided by Microsoft to solve stateless connections. The main problem of solving stateless connections is that the connection between users and server data uses SessionID, sessionID is stored in the browser, and SessionID is stored in the server, and SessionID is unique, thus implementing interconnection between them.
We use Memcached to simulate the Session service. This is also the principle. When a user logs on for the first time, a GUID is generated and saved on the Cookie. Then, the GUID is used as the key in Memcached, we just need to add the above directly. To make the simulated Session more flexible and realistic, our Value can use the Dictionary type. Of course, we need to encapsulate a class, and then write the corresponding method in the class.
When encapsulating this class, we need to note that Memcached is used at this time. What if Redis is used later? After that, we 'd better use the observer mode for the corresponding design. To achieve that configuration, we need to remove methods such as adding the observer, directly use spring.net to inject it into it.
The summary is not the best, but only suitable. Select an appropriate solution based on your project.
The session usage problems are lost for some reason.
Summary of Solutions to Session loss
Recently, when I was working on an ASP. NET project, the test website could not retrieve the value in the Session. I searched online and found some solutions, which are recorded here. Finally, the problem is solved using the method stored in StateServer.
SessionState Timeout), there are three main reasons.
I. Some antivirus software will scan your Web. Config file. At that time, the Session will definitely drop. This is Microsoft's statement.
2: The program contains code that causes Session loss and insufficient server memory.
3. The program has a framework page and Cross-Domain Information.
The first solution is to shield the antivirus software from scanning the Web. Config file (do not edit it when the program is running)
The second is to check whether the Code has Session. Abandon () or the like.
The third is to start ASP. NET State Service in the Window Service.
How to close all processes except System Processes
You can select the control panel, find the management tool, and double-click the service. I have a list where you can choose to disable Alerter and send an error alarm to the Administrator when the system fails, unless the computer is connected to the LAN and has a network administrator, it is generally not required. Error Reporting Service is used to report errors in Windows to Microsoft. Background intelligent transfer service smart transmission service in the Background. Implement information transmission between servers in http1.1. Microsoft said it supports resumable data transfer when Windows is updated. The Clipbook is used to share and cut contents with other computers in the LAN. You can use the Network Dynamic Data Exchange Service provided by Network DDE and Network dde dsdm to view the clipboard in the remote computer. Help and Support for Windows, which should be used by new users. The Indexing Service index Service can create an index for the document content and attributes on a local hard drive or shared network drive, and quickly locate the desired document through the system's unique document filter. It greatly enhances the search capability of Windows, but the indexing service needs to be continuously carried out, consuming a large amount of system resources. It is strongly recommended to disable it. Most IPSEC services users are not available. This is not MSN Messenger. It is a service used to send and receive messages from system administrators or by Alerter services. It is not used by household users. Net Login logs domain controller to verify the Login information service NetMeeting Remote Desktop Sharing use Netmeeting to achieve Remote Desktop Sharing, allowing others to access your computer through NetMeeting, for the sake of your computer and data security, we recommend that you disable it. Network DDE has not found its usefulness yet, except the Network clipboard. Network dde dsdm is similar to t lm Security Support Provider. If you do not run the Message Queuing or Talnet service, it is useless. Performance Logs and Alerts records the running status of the machine and regularly writes Logs or sends warnings. However, the content is too professional and is similar to tianshu for general users. Portable Media Serial Number can be used to retrieve the Serial Number from a Portable music player connected to your computer. In this case, it is recommended that you disable anything Microsoft has done to protect digital copyrights. QoS RSVP Service Quality Resource Reservation Protocol. It provides 20% of the reserved bandwidth for QoS (Service Quality) applications. For non-broadband users, this 20% of the bandwidth cannot be wasted. Remote Desktop Help Session Manager new features in Windows XP, Remote assistance. Remote Registry Service Remote Registry Access/modification. In this case, you must first disable one of the services. Routing and Remote Access. It is almost useless to allow users to connect to a local network through Modem or other devices using standard or VPN connections. Secondary Logon assigns specified operation permissions to users other than administrator! Allows Restricted Users to start programs or perform operations with higher permissions (such as administrator permissions. When you right-click a file, the "run as" command appears in the menu. If it is disabled, the command disappears. Smart Card/Smart Card Helper Smart Card support. SSDP Dicovery Service non-PNP hardware uses this Service, unless you have old hardware, it is not required. System Restore Service System Restoration Service, occupying a large amount of hard disk space and the System ...... the remaining full text>