简体   繁体   中英

what more I need to can storage in redis cache in c#?

I was installed XX from nuget and StackExchange.Redis.StrongName , also put the next configuration in the web.config RedisSessionStateProvider

<sessionState mode="Custom" customProvider="RedisSessionProvider"  cookieless="true" > <providers> <add name="RedisSessionProvider"  type="Microsoft.Web.Redis.RedisSessionStateProvider" port="6380" host="XXX.redis.cache.windows.net" accessKey="OQm………15E=" ssl="true" connectionTimeoutInMilliseconds="5000" operationTimeoutInMilliseconds="1000" retryTimeoutInMilliseconds="3000" writeExceptionsToEventLog="true" /></providers>

but can't storage the session on redis, but if I do the connection on code, it is succesfull. my code:

/// set the conecction

private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
    var redisOption = new ConfigurationOptions();

    return ConnectionMultiplexer.Connect("XXX.redis.cache.windows.net:6380,abortConnect=false,ssl=true,password=OQmAPmp0 . . . . TJE15E=");

});


///return connection object

public static ConnectionMultiplexer Connection
{
    get
    {
        return lazyConnection.Value;
    }
} 

///the session is created and added some elements manually in redis to test     

public ActionResult SessionStart()
{
    IDatabase cache = Connection.GetDatabase();
    Session["loginTime"] = DateTime.Now.ToString();
    string strValue = "myvalue";
    Session.Add("myvalue ", strValue);
    return View();
}

I need storage the session automatically in redis, please help me!

The solution was reinstall the packages RedisSessionStateProvider and StackExchange.Redis.StrongName from nuget, after that is need change some things in the web config

WEB.CONFIG

<sessionState mode="Custom" customProvider="MySessionStateStore" >
  <providers>
    <add name="MySessionStateStore"
     type="Microsoft.Web.Redis.RedisSessionStateProvider"
     host="abcde1234.redis.cache.windows.net"
     accessKey="FuDmzfO3B/6M1cX1ls="
     ssl="true" throwOnError="true" port="6380" writeExceptionsToEventLog="true"
     databaseId = "1"
     />
 </providers>
</sessionState>

CONTROLLER

And then only create a session object:

Session["test-" + Guid.NewGuid().ToString()] = DateTime.Now.ToLongDateString();

You can use Redis Desktop Manager for tracking the values or the Azure portal 在此处输入图片说明

the solution was gave by @juank.

@Luca Detomi you can review the answer

It can't be that automatic, your need one more extra library to marry Redis and ASP.NET, and a small web.config modification. For example, the following shall do if you are not tied to StackExchange.Redis https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore , or this uses StackExchange.Redis as in your case https://github.com/welegan/RedisSessionProvider

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM