Search code examples
c#asp.net-mvcredissession-state

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!


Solution

  • 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 enter image description here

    the solution was gave by @juank.

    @Luca Detomi you can review the answer