2

I am trying to connect to an instance of the Azure Redis Cache (Preview) from a Visual Studio Web Project.

I am getting the following error: "Could not load file or assembly 'Authorization token passed by user Invalid".

I have done the following: 1) Logged into Azure Portal and created a new Redis Cache PREVIEW 2) Opened Visual Studio and created New Project (Web MVC) 3) Manage Nuget Packages - Update All 4) Install Package - "Windows Azure Cache Version 2.2.0.0" 5) Open Web.Config, in dataCacheClients section to the following:

<dataCacheClients>
<dataCacheClient name="default">
  <autoDiscover isEnabled="true" identifier="mycache.redis.cache.windows.net"  />
    <securityProperties mode="Message" sslEnabled="false">
    <messageSecurity authorizationInfo="xxxxxxxmykeyxxxxxxxx"/>
  </securityProperties>
</dataCacheClient>
</dataCacheClients>

6) Changed HomeController.cs to the following:

using Microsoft.ApplicationServer.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CacheRedisTest.Controllers
{
public class HomeController : Controller
{
    static DataCacheFactory myFactory;
    static DataCache myCache;
    public ActionResult Index()
    {
        if (myCache == null)
        {
            myFactory = new DataCacheFactory();
            myCache = myFactory.GetDefaultCache();
        }

        return View();
    }
}
}

Do I need some other nuget packages specific to Redis? Also where do I put the port number which is mentioned in the Azure console?

Thanks for reading

Frank Cannon
  • 1,643
  • 2
  • 18
  • 29
  • 1
    It's easier than that, you don't need a DataCacheFactory - use ConnectionMultiplexer . See http://azure.microsoft.com/blog/2014/06/05/mvc-movie-app-with-azure-redis-cache-in-15-minutes/ – RickAndMSFT Jun 13 '14 at 17:41

2 Answers2

9

For Azure Redis Cache, you need to use a Redis client library like StackExchange.Redis. The "Windows Azure Cache" client library is specific to the Azure Managed Cache.

After adding the StackExchange.Redis NuGet package to your project, connect to Redis using the ConnectionMultiplexer object:

var cm  = ConnectionMultiplexer.Connect("mycache.redis.cache.windows.net,ssl=true,password=<password>");
var db = connection.GetDatabase();

db.StringSet("key", "value");
var key = db.StringGet("key");

Links with more information:

http://azure.microsoft.com/en-us/documentation/articles/cache-dotnet-how-to-use-azure-redis-cache/ https://github.com/StackExchange/StackExchange.Redis

Mike Harder
  • 276
  • 3
  • 5
  • It appears to connect using these instructions. The first time I did it however a few seconds later I got a dialog box saying "Find Source:SocketManager.Poll.cs" which I guess means it can't a certain file. – Frank Cannon May 14 '14 at 08:57
  • The dialog box saying "Find *.cs" is probably because an exception was thrown and VS was trying to load the corresponding source file. – Mike Harder May 15 '14 at 05:20
  • Hello guys, StackExchange.Redis client requires .NET Framework 4 or higher. I have a .NET Framework 3.5 project and I need to use Azure Redis Cache with it. But I can't find any client that supports .NET Framework 3.5. Can anybody help me? – Gev Nov 18 '15 at 16:58
2

You don't need dataCacheClients in your web.config - you wouldn't want to check a secret into your source. You'd configure it like this in a MVC controller

    public class MoviesController : Controller
   {
      private MovieDBContext db = newMovieDBContext();
      private static ConnectionMultiplexer connection;
      private static ConnectionMultiplexer Connection
      {
         get
         {
            if (connection == null || !connection.IsConnected)
            {
               connection = ConnectionMultiplexer.Connect(
               "<your Cache>.redis.cache.windows.net,ssl=true," +
               "password=<Your password>");
            }
            return connection;
         }
      }

Again, don't put your account/password in the source code - use ConfigurationManager.AppSettings["Account"], ConfigurationManager.AppSettings["Password"] - and store the values the Configure tab in the Azure portal

for more info see http://azure.microsoft.com/blog/2014/06/05/mvc-movie-app-with-azure-redis-cache-in-15-minutes/

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78