ASP.NET Session State Provider for Azure Redis Cache
Updated: February 20, 2015
Azure Redis Cache provides a session state provider that you can use to store your session state in a cache rather than in-memory or in a SQL Server database. To use the caching session state provider, first configure your cache, and then configure your ASP.NET application for cache using the Redis Cache Session State NuGet package.
To configure a client application in Visual Studio using the Redis Cache Session State NuGet package, right-click the project in Solution Explorer and choose Manage NuGet Packages.
Type RedisSessionStateProvider into the Search Online text box, select it from the results, and click Install.
Note The Redis Session State Provider NuGet package has a dependency on the StackExchange.Redis.StrongName package. If the StackExchange.Redis.StrongName package is not present in your project it will be installed. Note that in addition to the strong-named StackExchange.Redis.StrongName package there is also the StackExchange.Redis non-strong-named version. If your project is using the non-strong-named StackExchange.Redis version you must uninstall it, either before or after installing the Redis Session State Provider NuGet package, otherwise you will get naming conflicts in your project. For more information about these packages, see Configure .NET cache clients for Azure Redis Cache.
The NuGet package downloads and adds the required assembly references and adds the following adds the following section into your web.config file that contains the required configuration for your ASP.NET application to use the Redis Cache Session State Provider.
<sessionState mode="Custom" customProvider="MySessionStateStore"> <providers> <!-- <add name="MySessionStateStore" host = "127.0.0.1" [String] port = "" [number] accessKey = "" [String] ssl = "false" [true|false] throwOnError = "true" [true|false] retryTimeoutInMilliseconds = "0" [number] databaseId = "0" [number] applicationName = "" [String] connectionTimeoutInMilliseconds = "5000" [number] operationTimeoutInMilliseconds = "5000" [number] /> --> <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="127.0.0.1" accessKey="" ssl="false" /> </providers> </sessionState>
Configure the attributes with the values from your cache blade in the Azure Management Portal Preview, and configure the other values as desired.
Note For instructions on accessing your cache properties, see Review cache settings.
host– specify your cache endpoint.
port– use either your non-SSL port or your SSL port, depending on the
accessKey– use either the primary or secondary key for your cache.
ssl– true if you want to secure cache/client communications with ssl; otherwise false. Be sure to specify the correct
throwOnError– true if you want an exception to be thrown in the event of a failure, or false if you want the operation to fail silently. You can check for a failure by checking the static
Microsoft.Web.Redis.RedisSessionStateProvider.LastExceptionproperty. The default is true.
retryTimeoutInMilliseconds– Operations that fail are retried during this interval, specified in milliseconds. The first retry occurs after 20 milliseconds, and then retries occur every second until the
retryTimeoutInMillisecondsinterval expires. Immediately after this interval, the operation is retried one final time. If the operation still fails, the exception is thrown back to the caller, depending on the
throwOnErrorsetting. The default value is
0which means no retries.
databaseId– Specifies which database to use for cache output data. If not specified, the default value of
applicationName– Keys are stored in redis as
<AppName>_<SessionId>_Data. This enables multiple applications to share the same key. This parameter is optional and if you do not provide it a default value is used.
connectionTimeoutInMilliseconds– This setting allows you to override the
connectTimeoutsetting in the StackExchange.Redis client. If not specified, the default
5000is used. For more information, see StackExchange.Redis configuration model.
operationTimeoutInMilliseconds– This setting allows you to override the
syncTimeoutsetting in the StackExchange.Redis client. If not specified, the default
1000is used. For more information, see StackExchange.Redis configuration model.
Don’t forget to comment out the standard
InProcsession state provider section in your web.config.
<!-- <sessionState mode="InProc" customProvider="DefaultSessionProvider"> <providers> <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=188.8.131.52, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /> </providers> </sessionState> -->
Once these steps are performed, your application is configured to use the Redis Cache Session State Provider. When you use session state in your application, it will be stored in an Azure Redis Cache instance.
|Note that data stored in the cache must be serializable, unlike the data that can be stored in the default in-memory ASP.NET Session State Provider. When the Session State Provider for Redis is used, be sure that the data types that are being stored in session state are serializable.|