How to use the session in a SharePoint 2010 program

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

Many developers like to use the session in the ASP.net program to record some information about the currently logged-on user. However, in a SharePoint 2010 system, the session feature is disabled by default. If you use session in an application, you will most likely find that the code throws a "referenced object is not instantiated" exception. If you debug your code through Visual Studio, you can see that the Httpcontext.session property returns NULL.

To use session in a SharePoint 2010 program, developers face some potential problems. The SharePoint 2010 system is likely to be deployed as a server farm model that may contain multiple Web front-end servers, and the user's HTTP requests are distributed to any one of several Web front-end servers by the NLB mechanism. This requires developers to write "stateless" code as much as possible, meaning that the code cannot rely on a specific server because the developer has no way of determining that its code will always run on a server. It is possible for a user to browse a page when its request is assigned to a Web server A, and then when the user refreshes the page, the request is assigned to Web Server B.

In such a server farm model, any data that the code holds in the server's memory becomes meaningless as the next user refreshes and processes the server transfer requested by the user. This is also a typical challenge for writing large-scale distributed application code.

When writing a distributed application that is likely to run in a server farm, a dedicated distributed data storage or distributed caching scheme is typically introduced. For example, the famous memcached is dedicated to doing this thing.

Well, a little bit farther. In any case, SharePoint 2010, for some reason, disables the use of Session in code by default. The reason for the ban is likely to be to prevent developers from writing "stateful" code that relies too much on the session, because ASP.net's session is stored in the server's memory by default. Once the code is deployed to a distributed server farm environment, it is likely that they will be wrong because they rely on session data that is stored only in one server's memory.

"Don't be fooled, don't think we don't know asp.net session in addition to the default InProc mode, also supports stateserver and SQL Server mode! "Well, yes, ASP. NET session can actually be configured as StateServer and SQL Server mode. The StateServer mode is to provide a state-saving service for all servers in a server farm by using one of the session states services on a particular server, and SQL Server mode saves all state information to the SQL Server database. Both of these models are good at supporting the distributed server farm model.

So, for students who like to use session in code, we can boldly enable session by modifying the Web.config file of the Web application. But wait a minute, SharePoint 2010 has a built-in PowerShell directive that lets us easily enable session in the server farm. This instruction is enable-spsessionstateservice.

As shown in the figure above, open the SharePoint Management Shell and, if you enter the instructions above, you can automatically create a "Sharepoint_session_" on the SQL Server servers on which the SharePoint configuration database is located. State, and then automatically adds the corresponding entry in the web.config of all Web applications.

This automatically added entry indicates that the session will use SQL Server mode and the database connection string that holds the session. Open SQL Server Management Studio to find this automatically created database by Enable-spsessionstateservice, which contains only 2 table.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/web/sharepoint/

Enable-spsessionstateservice also provides several other parameters. The-defaultprovision parameter indicates that the existing metabase of the SharePoint system (the default is Sharepoint_config) will be used directly as the database hosting the session data, rather than using a dedicated database (which I highly recommend not). -databaseserver and-databasecredentials can specify that a custom database server be used, rather than the server that has the default placement configuration database, and the database connection account.

If you need to do a reverse operation, that is, the use of the session in the server farm is prohibited, you can use the Disable-spsessionstateservice directive.

Author: 51cto kaneb0y

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.