【问题标题】:How to connect to Azure Redis Cache如何连接到 Azure Redis 缓存
【发布时间】:2014-05-11 13:04:29
【问题描述】:

我正在尝试从 Visual Studio Web 项目连接到 Azure Redis 缓存(预览版)的实例。

我收到以下错误:“无法加载文件或程序集'用户传递的授权令牌无效”。

我做了以下事情: 1) 登录 Azure Portal 并创建一个新的 Redis Cache PREVIEW 2)打开Visual Studio并创建新项目(Web MVC) 3) 管理 Nuget 包 - 全部更新 4) 安装包 - “Windows Azure 缓存版本 2.2.0.0” 5)打开Web.Config,在dataCacheClients部分如下:

<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) 将 HomeController.cs 更改为以下内容:

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();
    }
}
}

我是否需要一些其他特定于 Redis 的 nuget 包?另外我应该把 Azure 控制台中提到的端口号放在哪里?

感谢阅读

【问题讨论】:

标签: asp.net redis azure-caching


【解决方案1】:

对于 Azure Redis 缓存,您需要使用像 StackExchange.Redis 这样的 Redis 客户端库。 “Windows Azure 缓存”客户端库特定于 Azure 托管缓存。

将 StackExchange.Redis NuGet 包添加到您的项目后,使用 ConnectionMultiplexer 对象连接到 Redis:

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");

更多信息的链接:

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

【讨论】:

  • 似乎使用这些说明进行连接。我第一次这样做但是几秒钟后我得到一个对话框说“查找源:SocketManager.Poll.cs”,我猜这意味着它不能是某个文件。
  • 出现“Find *.cs”的对话框可能是因为抛出了异常,VS试图加载对应的源文件。
  • 大家好,StackExchange.Redis 客户端需要 .NET Framework 4 或更高版本。我有一个 .NET Framework 3.5 项目,我需要使用 Azure Redis 缓存。但我找不到任何支持 .NET Framework 3.5 的客户端。有人可以帮帮我吗?
【解决方案2】:

您的 web.config 中不需要 dataCacheClients - 您不希望在源代码中检查秘密。你可以在 MVC 控制器中这样配置它

    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;
         }
      }

同样,不要将您的帐户/密码放在源代码中 - 使用 ConfigurationManager.AppSettings["账户"], ConfigurationManager.AppSettings["Password"] - 并将值存储在 Azure 门户中的“配置”选项卡

更多信息见http://azure.microsoft.com/blog/2014/06/05/mvc-movie-app-with-azure-redis-cache-in-15-minutes/

【讨论】:

  • 密码是“主”“键”吗??
猜你喜欢
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多