Session State Provider for Azure Managed Cache Service
Important
Microsoft recommends all new developments use Azure Redis Cache. For current documentation and guidance on choosing an Azure Cache offering, see Which Azure Cache offering is right for me?
The Microsoft Azure Cache session state provider is an out-of-process storage mechanism for ASP.NET applications. This provider enables you to store your session state in a cache rather than in-memory or in a SQL Server database.
For a sample of session state caching, see Cache Session State and Output Caching Sample.
Capabilities
The Microsoft Azure Cache session state provider has the following improvements over past ASP.NET session state providers:
It internally uses the NetDataContractSerializer class for session state serialization.
It can share session state among different ASP.NET applications.
It supports concurrent access to same set of session state for multiple readers and a single writer.
It can use compression through cache client properties.
The use of NetDataContractSerializer class for serialization results in the support of a wider range of serializable types. This includes support for binary serializable types.
The provider supports the ability for different ASP.NET applications to read and write the same session state data. For example say a developer has two different ASP.NET sites: /contoso and /adventureworks. The session state provider can be configured in each web application to point at the same session state store in the cache. As a result, both applications will read and write the same session data for any given session identifier.
The session state provider supports concurrent access to same set of session state for multiple readers and a single writer. Readers in this case are defined as pages that are marked as using ReadOnly session state. Applications that make extensive use of AJAX callbacks will not experience server-side request queuing if most of the application’s session state access patterns only require read-only access to session state.
To add the Session State provider to an ASP.NET project
First, configure a cache to use with the ASP.NET provider.
Use NuGet to add caching support to your ASP.NET project in Visual Studio.
Important
These steps require NuGet Package Manager version 2.6.40627.9000 or higher. To install the latest NuGet Package Manager, see NuGet Package Manager.
Remove any existing sessionState element that configures session state caching. Do not remove the commented sessionState section that was added by the NuGet package.
In the web.config file, uncomment the sessionState section.
<!-- Azure Caching session state provider --> <!--<sessionState mode="Custom" customProvider="AFCacheSessionStateProvider"> <providers> <add name="AFCacheSessionStateProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheSessionState"/> </providers> </sessionState>-->
The following list provides optional configuration changes that affect the session state provider.
Change the targeted named cache with the cacheName attribute of the add element.
Change the source of the cache client settings with the dataCacheClientName attribute of the add element. Set this attribute to the name of an existing dataCacheClient section in the web.config file.
Modify the behavior of the cache client by changing the settings in the associated dataCacheClient section. For example, enable compression with the isCompressionEnabled attribute.
<dataCacheClient name="default" isCompressionEnabled="true"> <!-- Other cache settings omitted --> </dataCacheClient>
For a list of available configuration options, see Cache Client Configuration Settings for Azure Managed Cache Service. Note that the ASP.NET providers for caching do not support binary or custom serialization types. If these serializers are used for session state, the following exception occurs: "Type 'Microsoft.Web.DistributedCache.SerializableSessionStateStoreData' in Assembly 'Microsoft.Web.DistributedCache, Version=101.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable"
Change the settings for the session state provider. For a list of available configuration options, see ASP.NET Session State Provider Configuration Settings for Azure Managed Cache Service.