Asp Net Session State Serialization
SQL Server In Memory OLTP as ASP. NET Session State Provider Site. Point. Special thanks to Matthew Wilkin for kindly helping to peer review this article. Internet applications use the HTTP protocol, following a request response paradigm. As each request is independent of the other, we need to find a way to maintain some information across multiple requests. For that, either we put all the common data in each request, or we put keys in requests to store the data on the server against each key. State management is a basic requirement for any web application. As a user interacts with an application, we need to maintain the users preferences the application state with its corresponding actions and responses, and potentially other metadata related to the application. Therefore, most of the web application frameworks today provide various out of the box features for state management. ASP. NET Session State Management. The ASP. NET framework is one of the most used frameworks for building web applications. It provides rich features and tools for rapid application development. Sessions are commonly used as a state management technique in numerous ASP. NET applications due to their simplicity and flexibility. Lets have a quick overview of session state management. At a high level, the diagram above shows there are two types of session modes In. Proc In Process and Out. Proc Out of Process. While using In. Proc mode, session data is stored in the web servers memory, while Out. Proc allows us to store the session data either in a State Server, SQL Server or at any other custom location. In. Proc is the fastest, as data is available in the web servers memory itself, with no serializationdeserialization or IO required. But this doesnt fit into a Web FarmWeb Garden scenario, commonly found in Enterprise applications, where SQL Server is used for storing Session State. A Web Farm is a group of web servers hosting the application, where a load balancer routes the request based on the load of a server. On the other hand, a Web Garden involves multiple worker processes w. Both provide a way to scale the application if traffic load cant be handled by one server or one worker process. Whenever the need for reliable sessions becomes a necessity, Enterprise applications tend to leverage the High Availability features found in SQL Server, such as redundancy and clusters. However, using SQL Server has an extra performance cost, because we need to serializede serialize session data while readingwriting to SQL. This requires an IO operation as data resides in disk based files. Session data is generally small in size but very dynamic in nature. In times of heavy load, many threads could be writing session data while others read the data. As the threads actively access the data in a small region, lock and latch contentions start creating issues. Download Driver Guitar Link For Windows 7 here. Reading and writing data starts to take more time, which makes for a bad user experience. So theres a trade off in each mode, but as discussed, we end up using SQL in many scenarios and bear the performance cost. For better performance, we invest more dollars by adding more powerful servers into the system, but are benefitted only to a certain extent, and all the available resources are not used optimally. What is In Memory OLTP In Memory OLTP is Microsofts relatively new online transaction processing technology, which was introduced with SQL Server 2. These tables dont suffer from latching, because the In Memory OLTP engine uses lock free algorithms and structures. Microsoft started working on a project with code name Hekaton that was aimed to provide 1. In SQL Server 2. 01. Theyve made a good amount of changes in SQL Server 2. So if we leverage this feature of SQL Server then we can almost mitigate the performance cost of IO bound SQL as Session state provider. Using SQL Server In Memory OLTP for Session State Management. To use In Memory OLTP, we can write our own custom provider. However, theres already one package available via Nu. Get that leverages this feature, so instead of rolling our own, well use it. Configuring this package is very straightforward. The Nu. Get package may be found on the Nu. Get site, or it can be installed using Package Manager Console with. PM Install Package Microsoft. Web. Session. State. Sql. In. Memory. This package performs the following changes to your application Adds an assembly reference of Microsoft. Web. Session. State. Sql. In. Memory. Updates the session. State configuration in your web. Sql. In. Memory. Provider lt session. State modeCustom custom. ProviderSql. In. Memory. Provider. Sql. In. Memory. Provider typeMicrosoft. Web. Session. State. Sql. In. Memory. Provider. Stringdata source initial catalogASPState. In. Memory User IDuser Passwordpassword. State Change the connection string accordingly. Screen Capture Windows. Provides a SQL script named ASPState. In. Memory. sql, which can be used to configure the In Memory database. Before executing, be sure to update the script with any configuration changes as noted below. Change the name of the database, which is by default named as ASPState. In. Memory. Replace. CREATE DATABASE ASPState. Brij Mishra looks at web app state management, and Microsofts InMemory OLTP, to provide ASP. NET with a faster Session State Provider. State management is the process by which you maintain state and page information over multiple requests for the same or different pages. ASP. NET interview questions and answers for freshersbeginners and experienced. Our advanced ASP. Net interview questions are very useful for experienced ASP. NET. 3 easy but vital things about Sessions in AJAX Apps. It is VERY important to include a Period if using a whole domain. DanRothDemonstratesWebAPIUsingVisualStudio2012RC_512.jpg' alt='Asp Net Session State Serialization' title='Asp Net Session State Serialization' />In. Memory to CREATE DATABASE lt your database name b. Configure the path of the database primary group. Replace. NAME ASPState. In. Memory, FILENAME D SQLdataASPState. In. Memorydata. mdf with NAME ASPState. In. Memory, FILENAME lt path of mdf file c. Change the file name that specifies the path for the memory optimized file group according to your own server. Replace. NAME ASPState. In. Memoryxtp, FILENAME D SQLdataASPState. In. Memoryxtp with NAME ASPState. In. Memoryxtp, FILENAME lt path of xtp file d. In Memory OLTP provides two durability options SCHEMAONLY in case of server restart, the tables would be recreated and previous data would be lost. It is faster and used when we have transient data. By default it is enabled. SCHEMAANDDATA in case of server restart, the schema and data are maintained, similar to disk based tables. To configure it, change the DURABILITY attribute from SCHEMAONLY to SCHEMAANDDATA. After execution, it creates a new database ASPState. In. Memory default with two tables as dbo. Sessions and dbo. Session. Items. Dealing with Expired Sessions. When we use SQL for session state management, we need to delete the expired session data. In the traditional way, we used an SQL Server Agent Job to delete the expired session periodically. Similarly here, we have a new Stored Procedure Delete. Expired. Sessions, which can be configured in a job to delete the expired sessions. By default, session timeout is 2. State tag in our web. The Key Benefits. In Memory OLTP will boost the performance when using SQL Server Session State by a factor of 3. SQL Server Session State. We can also leverage the following features of SQL using In Memory OLTP We can make our session highly available with the help of the SQL Server Always. On feature. We can also leverage the traditional way of making high availability using failover clustering. In order to do that, we need to make some changes in the script, like setting durability as SCHEMAANDDATA and changing the Id constraint for the Session. Generally, a download manager enables downloading of large files or multiples files in one session. Many web browsers. Heres a few notes about Session state InProc modeInProc session state is limited to the amount of memory available to the worker process. Introduction There are different types of state in an ASP. NET web application page state, session state, and application state. Page state is state that is specific. Why would you want to use serialization The two most important reasons are to persist the state of an object to a storage medium so an exact copy can be recreated. Sessionstart creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. This article explains in detail the Session State Management technique and its modes in ASP. Net. Items table. We can make use of the Geo redundancy feature of the SQL Server, which makes it durable and highly available.