Asp.net 如何连接到Azure Redis缓存
我正在尝试从VisualStudioWeb项目连接到Azure Redis缓存(预览)的实例 我收到以下错误:“无法加载用户传递的文件或程序集的授权令牌无效” 我已经做了以下工作: 1) 登录Azure门户并创建新的Redis缓存预览 2) 打开Visual Studio并创建新项目(Web MVC) 3) 管理Nuget包-全部更新 4) 安装包-“Windows Azure缓存版本2.2.0.0” 5) 打开dataCacheClients部分中的Web.Config,转到以下位置:Asp.net 如何连接到Azure Redis缓存,asp.net,redis,azure-caching,Asp.net,Redis,Azure Caching,我正在尝试从VisualStudioWeb项目连接到Azure Redis缓存(预览)的实例 我收到以下错误:“无法加载用户传递的文件或程序集的授权令牌无效” 我已经做了以下工作: 1) 登录Azure门户并创建新的Redis缓存预览 2) 打开Visual Studio并创建新项目(Web MVC) 3) 管理Nuget包-全部更新 4) 安装包-“Windows Azure缓存版本2.2.0.0” 5) 打开dataCacheClients部分中的Web.Config,转到以下位置: <
<dataCacheClients>
<dataCacheClient name="default">
<autoDiscover isEnabled="true" identifier="mycache.redis.cache.windows.net" />
<securityProperties mode="Message" sslEnabled="false">
<messageSecurity authorizationInfo="xxxxxxxmykeyxxxxxxxx"/>
</securityProperties>
</dataCacheClient>
</dataCacheClients>
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控制台中提到的端口号放在哪里
感谢阅读有关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");
var cm=ConnectionMultiplexer.Connect(“mycache.redis.cache.windows.net,ssl=true,password=”);
var db=connection.GetDatabase();
db.StringSet(“键”、“值”);
var key=db.StringGet(“key”);
更多信息链接:
这是一篇关于您的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;
}
}
公共类电影控制器:控制器
{
私有MovieDBContext db=newMovieDBContext();
专用静态连接多路复用器连接;
专用静态连接多路复用器连接
{
得到
{
if(connection==null | |!connection.IsConnected)
{
连接=连接多路复用器。连接(
.redis.cache.windows.net,ssl=true+
“password=”);
}
回路连接;
}
}
同样,不要将您的帐户/密码放入源代码-使用
ConfigurationManager.AppSettings[“帐户”],
ConfigurationManager.AppSettings[“Password”]-并将值存储在Azure门户的“配置”选项卡中
有关更多信息,请参见它似乎是使用这些说明连接的。第一次连接时,几秒钟后,我看到一个对话框,上面写着“查找源代码:SocketManager.Poll.cs”,我想这意味着它无法连接到某个文件。对话框上写着“查找*.cs”可能是因为引发了异常,VS试图加载相应的源文件。大家好,StackExchange.Redis客户端需要.NET Framework 4或更高版本。我有一个.NET Framework 3.5项目,需要使用Azure Redis缓存。但我找不到任何支持.NET Framework 3.5的客户端。有人能帮我吗?很简单除此之外,您不需要DataCacheFactory-UseConnectionMultiplexer。请参阅密码是“主”密钥吗??虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。李如果链接页面发生更改,nk only答案可能无效-