C# Class Amazon.SessionProvider.DynamoDBSessionStateStore

DynamoDBSessionStateStore is a custom session state provider that can be used inside of an ASP.NET application. Session state is saved inside a DynamoDB table that can be configured in the web.config. If the table does not exist the provider will create it during initialization with default read and write units set to 10 and 5 unless configured otherwise. If the table is created the application startup will block for about a minute while the table is being created. Example web.config entry setting up the session state provider. <sessionState mode="Custom" customProvider="DynamoDBSessionStoreProvider"gt; <providers> <add name="DynamoDBSessionStoreProvider" type="Amazon.SessionProvider.DynamoDBSessionStateStore" AWSAccessKey="YOUR_ACCESS_KEY" AWSSecretKey="YOUR_SECRET_KEY" Region="us-east-1" Table="ASP.NET_SessionState" /> </providers> </sessionState>

The schema for the table used to store session requires a string hash key with no range key. The provider will look up the name of the hash key during initialization so any name can be given for the hash key.

Below is a list of configuration attributes that can specified in the provider element in the web.config. Config Constant Use AWSAccessKey Access key used. This can be set at either the provider or in the appSettings. AWSSecretKey Secret key used. This can be set at either the provider or in the appSettings. Region Required string attribute. The region to use DynamoDB in. The default is us-east-1. Possible values are us-east-1, us-west-1, us-west-2, eu-west-1, ap-northeast-1, ap-southeast-1. Application Optional string attribute. Application is used to partition the session data in the table so it can be used for more than one application. Table Optional string attribute. The table used to store session data. The default is ASP.NET_SessionState. ReadCapacityUnits Optional int attribute. The read capacity units if the table is created. The default is 10. WriteCapacityUnits Optional int attribute. The write capacity units if the table is created. The default is 5. CreateIfNotExist Optional boolean attribute. CreateIfNotExist controls whether the table will be auto created if it doesn't exist. Default is true.

Inheritance: System.Web.SessionState.SessionStateStoreProviderBase
Show file Open project: scopely/aws-sdk-net

Public Methods

Method Description
CreateNewStoreData ( HttpContext context, int timeout ) : System.Web.SessionState.SessionStateStoreData

Creates a new SessionStateStoreData object to be used for the current request.

CreateUninitializedItem ( HttpContext context, string sessionId, int timeout ) : void

Creates an initial session record in the DynamoDB table.

DeleteExpiredSessions ( IAmazonDynamoDB dbClient ) : void

A utility method for cleaning up expired sessions that IIS failed to delete. The method performs a scan on the ASP.NET_SessionState table with a condition that the expiration date is in the past and calls delete on all the keys returned. Scans can be costly on performance so use this method sparingly like a nightly or weekly clean job.

DeleteExpiredSessions ( IAmazonDynamoDB dbClient, string tableName ) : void

A utility method for cleaning up expired sessions that IIS failed to delete. The method performs a scan on the table with a condition that the expiration date is in the past and calls delete on all the keys returned. Scans can be costly on performance so use this method sparingly like a nightly or weekly clean job.

Dispose ( ) : void

Empty implementation of the override.

DynamoDBSessionStateStore ( IAmazonDynamoDB ddbClient ) : System

Constructor for testing.

DynamoDBSessionStateStore ( string name, NameValueCollection config ) : System

Constructor for testing.

EndRequest ( HttpContext context ) : void

Empty implementation of the override.

GetItem ( HttpContext context, string sessionId, bool &locked, System.TimeSpan &lockAge, object &lockId, SessionStateActions &actionFlags ) : System.Web.SessionState.SessionStateStoreData

Returns read-only session-state data from the DynamoDB table.

GetItemExclusive ( HttpContext context, string sessionId, bool &locked, System.TimeSpan &lockAge, object &lockId, SessionStateActions &actionFlags ) : System.Web.SessionState.SessionStateStoreData

Returns session-state data from the DynamoDB table.

Initialize ( string name, NameValueCollection config ) : void

Initializes the provider by pulling the config info from the web.config and validate/create the DynamoDB table. If the table is being created this method will block until the table is active.

InitializeRequest ( HttpContext context ) : void

Empty implementation of the override.

ReleaseItemExclusive ( HttpContext context, string sessionId, object lockId ) : void

Releases a lock on an item in the session data store.

RemoveItem ( HttpContext context, string sessionId, object lockId, System.Web.SessionState.SessionStateStoreData item ) : void

Removes the session record for DynamoDB.

ResetItemTimeout ( HttpContext context, string sessionId ) : void

Updates the expiration date and time of an item in the DynamoDB table.

SetAndReleaseItemExclusive ( HttpContext context, string sessionId, System.Web.SessionState.SessionStateStoreData item, object lockId, bool newItem ) : void

Updates the session-item information in the session-state data store with values from the current request, and clears the lock on the data.

SetItemExpireCallback ( SessionStateItemExpireCallback expireCallback ) : bool

Provider returns false for this method.

Private Methods

Method Description
CreateTable ( ) : Table
DynamoDBSessionStateStore ( ) : System
GetConfigSettings ( NameValueCollection config ) : void
GetHashKey ( string sessionId ) : string

Combine application and session id for hash key.

GetSessionStoreItem ( bool lockRecord, HttpContext context, string sessionId, bool &locked, System.TimeSpan &lockAge, object &lockId, SessionStateActions &actionFlags ) : System.Web.SessionState.SessionStateStoreData

Get the session for DynamoDB and optionally lock the record.

SetupTable ( ) : void
UserAgentRequestEventHandler ( object sender, RequestEventArgs args ) : void
ValidateTable ( ) : void

Make sure existing table is valid to be used as a session store.

deleteItem ( string sessionId ) : void
deserialize ( HttpContext context, string serializedItems, int timeout ) : System.Web.SessionState.SessionStateStoreData
serialize ( System.Web.SessionState.SessionStateItemCollection items ) : string

Method Details

CreateNewStoreData() public method

Creates a new SessionStateStoreData object to be used for the current request.
public CreateNewStoreData ( HttpContext context, int timeout ) : System.Web.SessionState.SessionStateStoreData
context System.Web.HttpContext
timeout int
return System.Web.SessionState.SessionStateStoreData

CreateUninitializedItem() public method

Creates an initial session record in the DynamoDB table.
public CreateUninitializedItem ( HttpContext context, string sessionId, int timeout ) : void
context System.Web.HttpContext
sessionId string
timeout int
return void

DeleteExpiredSessions() public static method

A utility method for cleaning up expired sessions that IIS failed to delete. The method performs a scan on the ASP.NET_SessionState table with a condition that the expiration date is in the past and calls delete on all the keys returned. Scans can be costly on performance so use this method sparingly like a nightly or weekly clean job.
public static DeleteExpiredSessions ( IAmazonDynamoDB dbClient ) : void
dbClient IAmazonDynamoDB The AmazonDynamoDB client used to find a delete expired sessions.
return void

DeleteExpiredSessions() public static method

A utility method for cleaning up expired sessions that IIS failed to delete. The method performs a scan on the table with a condition that the expiration date is in the past and calls delete on all the keys returned. Scans can be costly on performance so use this method sparingly like a nightly or weekly clean job.
public static DeleteExpiredSessions ( IAmazonDynamoDB dbClient, string tableName ) : void
dbClient IAmazonDynamoDB The AmazonDynamoDB client used to find a delete expired sessions.
tableName string The table to search.
return void

Dispose() public method

Empty implementation of the override.
public Dispose ( ) : void
return void

DynamoDBSessionStateStore() public method

Constructor for testing.
public DynamoDBSessionStateStore ( IAmazonDynamoDB ddbClient ) : System
ddbClient IAmazonDynamoDB
return System

DynamoDBSessionStateStore() public method

Constructor for testing.
public DynamoDBSessionStateStore ( string name, NameValueCollection config ) : System
name string
config System.Collections.Specialized.NameValueCollection
return System

EndRequest() public method

Empty implementation of the override.
public EndRequest ( HttpContext context ) : void
context System.Web.HttpContext
return void

GetItem() public method

Returns read-only session-state data from the DynamoDB table.
public GetItem ( HttpContext context, string sessionId, bool &locked, System.TimeSpan &lockAge, object &lockId, SessionStateActions &actionFlags ) : System.Web.SessionState.SessionStateStoreData
context System.Web.HttpContext
sessionId string
locked bool
lockAge System.TimeSpan
lockId object
actionFlags SessionStateActions
return System.Web.SessionState.SessionStateStoreData

GetItemExclusive() public method

Returns session-state data from the DynamoDB table.
public GetItemExclusive ( HttpContext context, string sessionId, bool &locked, System.TimeSpan &lockAge, object &lockId, SessionStateActions &actionFlags ) : System.Web.SessionState.SessionStateStoreData
context System.Web.HttpContext
sessionId string
locked bool
lockAge System.TimeSpan
lockId object
actionFlags SessionStateActions
return System.Web.SessionState.SessionStateStoreData

Initialize() public method

Initializes the provider by pulling the config info from the web.config and validate/create the DynamoDB table. If the table is being created this method will block until the table is active.
public Initialize ( string name, NameValueCollection config ) : void
name string
config System.Collections.Specialized.NameValueCollection
return void

InitializeRequest() public method

Empty implementation of the override.
public InitializeRequest ( HttpContext context ) : void
context System.Web.HttpContext
return void

ReleaseItemExclusive() public method

Releases a lock on an item in the session data store.
public ReleaseItemExclusive ( HttpContext context, string sessionId, object lockId ) : void
context System.Web.HttpContext The HttpContext for the current request.
sessionId string The session identifier for the current request.
lockId object The lock identifier for the current request.
return void

RemoveItem() public method

Removes the session record for DynamoDB.
public RemoveItem ( HttpContext context, string sessionId, object lockId, System.Web.SessionState.SessionStateStoreData item ) : void
context System.Web.HttpContext
sessionId string
lockId object
item System.Web.SessionState.SessionStateStoreData
return void

ResetItemTimeout() public method

Updates the expiration date and time of an item in the DynamoDB table.
public ResetItemTimeout ( HttpContext context, string sessionId ) : void
context System.Web.HttpContext
sessionId string
return void

SetAndReleaseItemExclusive() public method

Updates the session-item information in the session-state data store with values from the current request, and clears the lock on the data.
public SetAndReleaseItemExclusive ( HttpContext context, string sessionId, System.Web.SessionState.SessionStateStoreData item, object lockId, bool newItem ) : void
context System.Web.HttpContext The HttpContext for the current request.
sessionId string The session identifier for the current request.
item System.Web.SessionState.SessionStateStoreData The SessionStateStoreData object that contains the current session values to be stored.
lockId object The lock identifier for the current request.
newItem bool true to identify the session item as a new item; false to identify the session item as an existing item.
return void

SetItemExpireCallback() public method

Provider returns false for this method.
public SetItemExpireCallback ( SessionStateItemExpireCallback expireCallback ) : bool
expireCallback SessionStateItemExpireCallback
return bool