ASP. NET session state provider for Azure Redis cache

Source: Internet
Author: User
Tags redis version

Azure Redis Cache provides a session state provider that you can use to store session state in the cache instead of in memory or in a SQL Server database. To use a cached session state provider, first configure the cache, and then use the Redis cache session state NuGet package to configure the ASP. NET application for caching.

storing ASP. NET session state in cache
  1. To configure applications in Visual Studio by using the Redis Cache Session State NuGet Package, right-click the project in Solution Explorer and select Manage NuGet packages.

  2. Will redissessionstateprovider Type the online search text box, select from the results, and click Install .

    Note
    The Redis Session State Provider NuGet package is dependent on the StackExchange.Redis.StrongName package. If the StackExchange.Redis.StrongName package is not displayed in the project, it will be installed. Note that in addition to the strong-named StackExchange.Redis.StrongName package, there is also a stackexchange.redis non-strong-name version. If your project uses a non-strong named Stackexchange Redis version, you must uninstall it before or after installing the Redis Session State Provider NuGet Package, or you will get the name of the conflict in the project. For more information about these packages, see Configuring the. NET cache Client for Azure Redis cache.


    The NuGet package downloads and adds the required assembly reference, and then adds the following session to the Web. config file, which contains the configuration required by the ASP. NET application to use the Redis Cache session state provider.

    Xml
      <sessionstate mode= "Custom" customprovider= "Mysessionstatestore" > <providers> <!--& Lt;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 = "[number]           "         Operationtimeoutinmilliseconds = "[number]         "/>-- <add name= "Mysessionstatestore" type= "Microsoft.Web.Redis.RedisSessionStateProvider" host= "127.0.0.1" AccessKey = "" ssl= "false"/> </providers> </sessionState>
    The comments section provides examples of properties and presentation settings for each property.

  3. Configure this value for the properties in the Azure Management Portal Preview Cache Sidebar, and configure the additional values that you want.

    Note
    For instructions on accessing the cache properties, see Review cache settings.

    • host– Specify the cache endpoint.

    • portssl Use a non-SSL port or SSL port, depending on the settings.

    • accessKey– Use the cached master key or secondary key.

    • sslTrueif you want to protect the cache/client from SSL communication, otherwise false. Make sure that you specify the correct port .

      Important Notes
      By default, non-SSL ports are disabled for the new cache. Specify this setting to true to use the SSL port. For more information about enabling non-SSL ports, see Access Ports in the Configure cache in Azure Redis cache topic.

    • throwonerror  – if an exception is thrown due to a failure, &NBSP; true ; false . By selecting the Microsoft.Web.Redis.RedisSessionStateProvider.LastException   property check for failure. The default value is &NBSP; true .

    • retryTimeoutInMilliseconds– The failed operation will be retried during this period (in milliseconds). After the first retry occurs 20 milliseconds, retry every second until the retryTimeoutInMilliseconds interval expires. Retry the last operation immediately after this interval. If the operation still fails, the throwOnError exception condition is returned to the calling program, depending on the setting. The default value is 0 , which means no retries.

    • databaseId– Specify the database that is used to cache the output data. If not specified, the default value is used 0 .

    • applicationName– The key is stored as in Redis <AppName>_<SessionId>_Data . This will allow multiple applications to share the same key. This parameter is optional and if not provided, the default value is used.

    • connectiontimeoutinmilliseconds  – This setting allows you to overwrite   in the Stackexchange.redis client; connecttimeout   settings. If not specified, use &NBSP;   The default &NBSP; connecttimeout   Settings. For more information, see the  stackexchange.redis configuration model on TechNet.

    • operationTimeoutInMilliseconds– This setting allows you to override the settings in the Stackexchange.redis client syncTimeout . If not specified, 1000 The default setting is used syncTimeout . For more information, see the Stackexchange.redis configuration model on TechNet.

    For more information about these properties, seeAn advertisement that is published in the original blog on the ASP. NET session state provider advertisement for Redis.

  4. Please do not forget to comment out the standard InProc Session state provider in the Web. config file.

    Xml
    <!--<sessionstate mode= "InProc"       customprovider= "Defaultsessionprovider" >      <providers>        <add name= "Defaultsessionprovider"               type= "System.Web.Providers.DefaultSessionStateProvider,                     System.Web.Providers, version=1.0.0.0, culture=neutral,                     publickeytoken=31bf3856ad364e35 "               Connectionstringname= "DefaultConnection"/>      </providers>  </sessionState>--

Once these steps are performed, the application is configured to use the Redis cache session state provider. When you use a session in the application, it is stored in the Azure Redis Cache instance.

ASP. NET session state provider for Azure Redis cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.