. Net sites in Windows environments with Nginx and Redis for load Balancing Series (iii)

Source: Internet
Author: User

. Net sites use Redis storage session information to implement multiple site common session information with Redis

1. Create a new Web project, I am creating an empty MVC project.

Add the home controller after you've created it, and create a new index action method and view view file.

2. Add Redissessionstateprovider this reference using NuGet.

After the installation is complete, three references are added automatically:

Microsoft.Web.RedisSessionStateProvider

StackExchange.Redis.StrongName

Stackexchange.redis

Web. config automatically adds the following configuration file

<sessionstate mode="Custom"Customprovider="Mysessionstatestore"> <providers> <!--for more details check https://Github.com/azure/aspnet-redis-providers/wiki --<!--either use'connectionString'OR'Settingsclassname'and'Settingsmethodname'OR use'Host','Port','AccessKey','SSL','Connectiontimeoutinmilliseconds'and'Operationtimeoutinmilliseconds'. -<!--'throwOnError','Retrytimeoutinmilliseconds','databaseId'and'ApplicationName'Can is used with both options. -<!--<add Name="Mysessionstatestore"Host="127.0.0.1"[String] Port=""[number] AccessKey=""[String] SSL="false"[true|false] throwOnError="true"[true|false] Retrytimeoutinmilliseconds=" the"[number] databaseId="0"[number] ApplicationName=""[String] Connectiontimeoutinmilliseconds=" the"[number] Operationtimeoutinmilliseconds=" +"[number] connectionString="<valid Stackexchange.redis Connection string>"[String] Settingsclassname="<assembly qualified class name that contains settings method specified below. Which basically return ' connectionString ' value>"[String] Settingsmethodname="<settings method should is defined in Settingsclass. It should be public, static, does not take any parameters and should has a return type of ' String ', which is basically ' C Onnectionstring ' value.>"[String] Loggingclassname="<assembly qualified class name that contains logging method specified below>"[String] Loggingmethodname="<logging method should is defined in Loggingclass. It should is public, static, does not take any parameters and should has a return type of system.io.textwriter.>"[String]/>--<add name="Mysessionstatestore"Type="Microsoft.Web.Redis.RedisSessionStateProvider"host=""accesskey=""Ssl="true"/> </providers> </sessionState>

Host to the IP address of your Redis service, my is 127.0.0.1,ssl to false, port number if not the default port, you can modify port= "XXXX".

3. Add the test code for the session in the index method to test whether Redis saves the data successfully. If you are saving a complex type, add a serializable label [Serializable], as shown in my test code

 Public classHomecontroller:controller {Private Static string_websitestr;  Public Static stringWebSite {Get            {                if(string. Isnullorwhitespace (_WEBSITESTR)) {_websitestr= system.configuration.configurationmanager.appsettings["WebSite"]; }                return_websitestr; }        }        //Get:home         PublicActionResult Index () {system.web.httpcontext.current.session["firsttest"] ="1231"; Student P=NewStudent () { age= -, name="liuyu7177"            }; system.web.httpcontext.current.session["Student"] =p; returnView (); }        //Get:home         PublicContentresult () {varstr = (string) system.web.httpcontext.current.session["firsttest"] +WebSite; varp = (Student) system.web.httpcontext.current.session["Student"]; returnContent (str); }} [Serializable] Public classStudent { Public stringName {Get;Set; }  Public intAge {Get;Set; } }
View Code

Where website this static property is used when publishing multiple sites to differentiate access to that site.

Add a node in Web. config <add key= "WebSite" value= "WebSite1"/>

Start debugging, if the/home/two page successfully returns to the contents of the session, the use of Redis storage session data success.

4. Publishing two or more two sites

Hang two sites on IIS with the newly created project. Create a new two publish directory, change the Web. config file, a value set WebSite1, and a value set WebSite2.

The first site is named Redissessiontest, and the bound IP is 127.0.0.1 port is 88

The second site is named Redissessiontesttwo, and the bound IP is 127.0.0.1 port is 89

Open the browser to visit separately: Http://127.0.0.1:88/Home/Index,http://127.0.0.1:89/Home/Two,http://127.0.0.1:88/Home/Two

The first site returns 1231WEBSITE1, and the second site returns 1231WEBSITE2, indicating that the session was shared successfully.

There are two issues to note:

1. This two site is under the same top-level domain name.

2. The second site does not open the Home/index page, otherwise it is not possible to determine whether the second site and the first site sharing session.

Finally: Redis to install a newer version, SSL should be set to false, otherwise it may be reported No connection is available to service this Operation:eval error

Back to navigation bar

. Net sites in Windows environments with Nginx and Redis for load Balancing Series (iii)

Related Article

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.