Question

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

Était-ce utile?

La solution

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

Autres conseils

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/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top