SharePoint learning notes 1-asp.net and SharePoint session mechanism

Source: Internet
Author: User
Tags management studio sql server management studio

1. Three session mechanisms of Asp.net

In ASP. NET OfProgramTo use Session Object, make sure that @ Page Instruction in progress Enablesessionstate Attribute is True Or Readonly And Web. config The file is correctly set Sessionstate Attribute.

ASP. NET Medium Session The status Web. config Marked under the mark in the file Mode Attribute. This property has four possible values: Off , Inproc , StateServer And Sqlserver .

SetOffDisabledSession.

Inproc Is the default setting , This mode and the previous ASP The method of session status is similar. , The session status is saved in ASP. NET In the process, its advantages are obvious: performance. Data access within a process is naturally faster than that of a process. However, this method Session The status of depends on ASP. NET Process, when IIS When a process crashes or is restarted normally, the State stored in the process is lost.

To overcomeInprocDisadvantages of the mode,ASP. NETTwo methods are provided to maintain the session status outside the process.

ASP. NET First, provide Windows Service: Aspstate, After the service is started, ASP. NET The application can Mode Set property "Sateserver ", To use this Windows The status management method provided by the Service.

Besides Web. config File Settings Mode The property is StateServer You must also set the run StateServer Server IP Address and port number . If IIS Running on the machine StateServer Then IP The address is 127.0.0.1, The port number is usually 42424. The configuration is as follows:

Mode = "StateServer"

stateconnectionstring =" TCPIP = 127.0.0.1: 42424 "

Use this mode,Session state storage will not depend onIISProcess failure or restart,The session status will be stored inStateServerThe memory of the process.

Another session Status mode is Sqlserver Mode. This mode saves the session Status in SQL Server In the database. You must have at least one SQL Server Server, and create the required tables and stored procedures in the server. . Net SDK Two scripts are provided to simplify this job: Installsqlstate. SQL And Uninstallsqlstate. SQL . Files in these two countries are stored in the following path:

Winntmicrosoft. netframework

To configureSQL ServerServer, which can be run in the command lineSQL ServerProvided command line toolsOsql.exe

Osql-s [server name]-U [user]-P [Password]

For example:

Osql-s (local)-u As-P ""-I installsqlstate. SQL

After necessary database preparation Web. config File Sessionstate Element Mode Attribute changed "Sqlserver ", And specify SQL The connection string. The details are as follows:

Mode = "sqlserver"

Sqlconnectionstring = "Data Source = 127.0.0.1; userid = sa; Password =; trusted _Connection= Yes"

Use Sqlserver Mode. Session The status does not depend on IIS In addition to servers SQL Server Makes the status storage independent from a single SQL Server, In this way, the application can be highly reliable.

 

2. How to Use session in SharePoint

 

Many developers like to use session in ASP. net programs to record information about the currently logged-on users. However, in SharePoint 2010, the session function is disabled by default. If a session is used in an application, you may find thatCodeWill throw a "the referenced object is not instantiated" exception. If you debug the Code through Visual Studio, you can find that the returned value of httpcontext. Session is null.

 

To use session in a Sharepoint 2010 Program, developers will face some potential problems. The SharePoint 2010 system may be deployed as a server farm. It may contain multiple Web Front-end servers, users' HTTP requests are distributed to any server of multiple Web Front-end servers by the NLB mechanism. This requires developers to write "stateless" code as much as possible. That is to say, the Code cannot depend on a specific server, because developers cannot determine that their code will always run on a server. When a user browses a page, the request is allocated to Web Server A. When the user refreshes the page, the request is allocated to Web Server B.

In such a server farm model, any data stored by the code in the server's memory will be transferred from the server that processes user requests after the next user refresh, which makes no sense.

For some reason, Sharepoint 2010 prohibits the use of sessions in code by default. The reason for disallow is probably to prevent developers from writing "stateful" code that is too dependent on sessions, because ASP. NET sessions are stored in the server's memory by default. Once these codes are deployed in the distributed server farm environment, they are likely to encounter errors due to dependency on session data stored only in the memory of a server.

ASP. NET sessions can be configured in StateServer and sqlserver mode. The StateServer mode uses a session state service on a specific server to provide the state storage service for all servers in the server farm, in sqlserver mode, all status information is saved to the SQL Server database. Both modes support the distributed server farm model.

Therefore, you can manually enable the session by modifying the Web. config file of the Web application. However, Sharepoint 2010 has a built-in powershell command, allowing us to easily enable session in the server farm. This command is enable-spsessionstateservice.

 

As shown in, open the SharePoint 2010 management shell and enter the preceding command to automatically create a database named sharepoint_session_state on the SQL server where the SharePoint configuration database is located, then. automatically add corresponding entries in config.

 

This automatically added entry indicates that the session will use the sqlserver mode and the database connection string that stores the session. Open SQL Server Management studio to find the database automatically created by enable-spsessionstateservice, which contains only two tables.

Enable-spsessionstateservice also provides several other parameters. -The defaultprovision parameter indicates that the existing configuration database (sharepoint_config by default) of the SharePoint system will be used directly as the database for storing session data, instead of using a dedicated database (I do not recommend this method ). -Databaseserver and-databasecredentials can specify the User-Defined Database Server (instead of the server where the database is configured by default) and the database connection account. If you need to perform a reverse operation to disable session use on the server farm, you can use the disable-spsessionstateservice command.

 

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.