(ix) Net core project using session and Redis for distributed

Source: Internet
Author: User
Tags httpcontext

First, Introduction

1, because the net core default is not the start session function, if need to use, need to open through the code.

2, this article explains if the default session implementation is enabled, that is, the session is stored in memory.

3, this extension explains how to use Redis to do Sessoin server, realize the distributed.

Second, open the default session function

1, write an API, write the session first, then read the session back.

2, startup does not start the session function, see the effect, error.

3, add two in startup to enable session code, see the effect, normal. here is the knowledge point, usesession need to write before USEMVC, otherwise will error.

4, Usesession write in one of the USEMVC, and then see the effect, error.

API code

     Public class Onecontroller:controller    {        publicstring GetString (string  id)        {            HttpContext.Session.SetString (ID, Guid.NewGuid (). ToString ());             return HttpContext.Session.GetString (ID);        }    }

Session not enabled, viewing effects

Enable session, the code is simple, you can knock on the line.

View Effects

Usesession, back down.

See the results and make a mistake again

Third, using Redis to do the session service

1, Net Core has already done the session of the Redis implementation, only need to open on it.

2, first to download the installation of Redis, if not, on the line with the landlord, connected in the code, just for testing purposes. Redis does not require a user name password by default.

3, again is the need to download a Redis client, I use Redisdesktopmanager

4. Add Redis configuration information to startup.

5. Check the running effect: the difference of Redis database before and after session creation.

Startup configuration Code

         Public void configureservices (iservicecollection services)        {            // Just this line of code.             "bitdao.cn:1012,abortconnect=false,connectretry=3,connecttimeout=3000,defaultdatabase= 1,synctimeout=3000,version=3.2.1,responsetimeout=3000");            Services. Addsession ();            Services. Addmvc ();        }

Before session creation

Access API Creation Session

After the session is created

Four, the meaning of Redis to do session service

1. The session principle is to save a sessionid in a cookie.

2, distributed deployment, Server A when writing a session, there is only a server in memory. When the user requests the second time, it may access to Server B, only to use SessionID to read the session is a null value.

3, if the session server with Redis, regardless of user access is assigned to which server, will use SessionID to Redis to take the session, can be taken to the value.

See figure below

V. Summary

1, Net Core default does not start Sessoin, need to correctly configure the boot session

2, distributed deployment, you can use Redis as a session server, Net Core has been implemented, a line of configuration code.

(ix) Net core project using session and Redis for distributed

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.