Session State Provider for Azure In-Role Cache

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 session state provider is an out-of-process storage mechanism for ASP.NET applications. This provider enables you to store your session state in an cache rather than in-memory or in a SQL Server database.

For a sample of session state caching, see Caching Session State and Output Caching Sample.

Capabilities

The 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 cluster. 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

  1. First, configure a cache to use with the ASP.NET provider. You can host caching with a co-located topology or with a dedicated role.

  2. Use NuGet to add caching support to your ASP.NET project in Visual Studio. For instructions, see How to: Prepare Visual Studio to Use Azure In-Role Cache.

    Important

    These steps require the latest NuGet Package Manager (version 2.1.31002.9028 or higher). To install the latest NuGet Package Manager, go to https://go.microsoft.com/fwlink/?LinkId=240311.

  3. Remove any existing sessionState element that configures session state caching. Do not remove the commented sessionState section that was added by the NuGet package.

  4. 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 output cache 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">
    

    For a list of available configuration options, see In-Role Cache Client Configuration Settings (Web.config). 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 in Azure In-Role Cache.

In This Section

See Also

Concepts

ASP.NET 4 Cache Providers for Azure In-Role Cache