AddToSession Overview

AddToSession is used to add a session variable that you will want to reference later. This is useful if you want a certain value to be accessed after a few pages, but won’t be carrying that information forward via query strings.

Session variables are special variables that exist only while the user's session with your application is active. Session variables are specific to each visitor to your site. They are used to store user-specific information that needs to be accessed by multiple pages in a web application. Unlike a cookie, the information is not stored on the users computer.

The default behavior is to store the session variables in the memory space of the Internet Information Services (IIS) worker process. If you are using this default in-memory session management, the session variables will be cleared when worker process recycles. IIS Worker Process Recycling is the process whereby IIS kills off the child processes that it spawns to handle incoming requests and starts clean copies of them. Over time, problems could arise in the processing (memory leaks in the application code, undisposed resources, etc.) that IIS wants to clean up without having to shutdown the server. So it will periodically tell the worker process to die off, and spawn a new one.

The user’s IIS session ends when the worker process is killed. This happens on the session timeout (timeout set in IIS on application pool) or when closing the browser. After this point the session variables saved previously will be lost. If you need the variables to be around for longer than this, consider using cookies.

If you would like to override a session variable, this can be accomplished with another AddToSession service with the same variable name. The value will be replaced with the new session variable value.

Overriding an Existing Variable

RemoveFromSession is an available service, but it not often used because you do not need to first remove the old variable in order to add a new one. AddToSession can be used to override the existing value with a new one.

In this example, we have a button process which sets a variable, then overrides it. We will then retrieve the value and display the value to the user in a message.


Here is the message that displays after the button is clicked and runs that process:

Here in the logs we can see that both AddToSession services were successful, so the last run AddToSession service is the last time the variable was edited, leaving it at that value.