繁体   English   中英

我该如何解决这个错误 StackExchange.Redis.RedisConnectionException: UnableToConnect on redisaec.redis.cache.windows.net:6380/Interactive

[英]how can I solve this error StackExchange.Redis.RedisConnectionException: UnableToConnect on redisaec.redis.cache.windows.net:6380/Interactive

运行项目后,我在我的项目中使用了 Regis 缓存我遇到了这个错误,我找不到这个问题的任何解决方案。

这是我的控制器代码

public ActionResult Create(Guid orgId, Guid? workflowId, Guid? directoryId)
        {
            IDatabase cache = CacheExtensions.Connection.GetDatabase();

            var processFlowData = this.db.Workflows.Find(workflowId);
            List<WorkflowNode> nodeListBeforeUpdate = null;
            List<WorkflowLink> linkListBeforeUpdate = null;
            if (processFlowData != null)
            {
                nodeListBeforeUpdate = processFlowData.WorkflowNodes.ToList();
                linkListBeforeUpdate = processFlowData.WorkflowLinks.ToList();
                ViewBag.oldNodeList = nodeListBeforeUpdate.Count();
                ViewBag.oldLinkList = linkListBeforeUpdate.Count();
            }
            //var workflowData = this.DatabaseFactory.ProcessFlowUtils.GetWorkflowData(workflowId.GetValueOrDefault());
            var appuser = this.GetDefaultUser();
            ViewBag.AppuserName = appuser.AppUserFullName;
            ViewBag.DirectoryId = directoryId;
            ViewBag.OrgId = orgId;
            ViewBag.IsNew = !workflowId.HasValue;
            if (!workflowId.HasValue)
            {
                workflowId = Guid.NewGuid();
                ViewBag.IsAgentExists = false;
            }
            else
            {
                //Considering single deployment
                SetSubscriptionStatus(orgId);
                var agentIdentification = this.DatabaseFactory.AgentUtils.GetDeploymentAgentItem(workflowId, orgId);
                ViewBag.AgentIdentificationGuid = agentIdentification.AgentIdentificationGuid;
                ViewBag.DeploymentStatus = agentIdentification.DeployStatus ?? 1;
                ViewBag.CanExecute = agentIdentification.canExecute;

                if (agentIdentification != null && agentIdentification.AgentDeploymentId != Guid.Empty)
                {
                    var isAgentExists = this.DatabaseFactory.ProcessFlowUtils.IsAgentExist(agentIdentification.AgentIdentificationGuid, orgId);
                    ViewBag.IsAgentExists = isAgentExists;
                }
                else
                {
                    ViewBag.IsAgentExists = false;
                }
            }
           
            ViewBag.WorkFlowId = workflowId;
            cache.Set("WorkflowID", workflowId, TimeSpan.FromDays(1));
            return View();
        } 

这是缓存扩展代码。

public static class CacheExtensions
    {
        private static Lazy<ConnectionMultiplexer> redisConnection = GetNewRedisConnection();
        public static LazyThreadSafetyMode LazyThreadSafetyMode = LazyThreadSafetyMode.None;
        private static Lazy<ConnectionMultiplexer> GetNewRedisConnection()
        {
            return new Lazy<ConnectionMultiplexer>(() =>
            {
                return ConnectionMultiplexer.ConnectAsync(ConfigurationManager.AppSettings["RedisConnection"]).ConfigureAwait(false).GetAwaiter().GetResult();
            }, LazyThreadSafetyMode);
        }

        public static ConnectionMultiplexer Connection
        {
            get
            {
                return redisConnection.Value;
            }
            set
            {
                if (value == null)
                    redisConnection = GetNewRedisConnection();
            }
        }

        public static T Get<T>(this IDatabase cache, string key)
        {
            return Deserialize<T>(cache.StringGet(key));
        }

        public static object Get(this IDatabase cache, string key)
        {
            return Deserialize<object>(cache.StringGet(key));
        }

        public static byte[] GetBytes(this IDatabase cache, string key)
        {
            return cache.StringGet(key);
        }
        public static void Set(this IDatabase cache, string key, object value)
        {
            int convertedVal;
            var expTimeSpan = ConfigurationManager.AppSettings["RedisExpiryTime"];
            bool isNumerical = int.TryParse(expTimeSpan, out convertedVal);
            cache.StringSet(key, Serialize(value),
                isNumerical == true ? TimeSpan.FromHours(convertedVal) : TimeSpan.FromHours(24));
        }
        public static void SetByteArray(this IDatabase cache, string key, byte[] value)
        {
            int convertedVal;
            var expTimeSpan = ConfigurationManager.AppSettings["RedisExpiryTime"];
            bool isNumerical = int.TryParse(expTimeSpan, out convertedVal);
            cache.StringSet(key, Serialize(value),
                isNumerical == true ? TimeSpan.FromHours(convertedVal) : TimeSpan.FromHours(24));
        }
        public static void Set(this IDatabase cache, string key, object value, TimeSpan timeToLive)
        {
            cache.StringSet(key, Serialize(value), timeToLive);
        }
        static byte[] Serialize(object o)
        {
            if (o == null)
            {
                return null;
            }
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, o);
                byte[] objectDataAsStream = memoryStream.ToArray();
                return objectDataAsStream;
            }
        }

        static T Deserialize<T>(byte[] stream)
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            if (stream == null)
                return default(T);

            using (MemoryStream memoryStream = new MemoryStream(stream))
            {
                T result = (T)binaryFormatter.Deserialize(memoryStream);
                return result;
            }
        }
    }

问题的完整细节是: Redisaec.redis.cache.windows.net 上的 UnableToConnect : 6380/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync,outstanding: 0, last-read: 0s ago, last-write: 0s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 0s ago, v: 2.2.50.36290 描述:执行当前 Web 请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:StackExchange.Redis.RedisConnectionException: UnableToConnect on redisaec.redis.cache.windows.net:6380/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync,outstanding: 0, last-read: 0s ago, last-写入:0 秒前,保持活动状态:60 秒,状态:正在连接,管理器:10 个可用的 10 个,最后一次心跳:从不,全局:0 秒前,v:2.2.50.36290

我不知道您的项目中RedisConnection的格式是什么。

它应该如下所示:

yourredisname.redis.cache.windows.net:6380,password=13l***yt8=,ssl=True,abortConnect=False,allowAdmin=true

我看到您添加了 asp.net 核心标签。 之前我为 redis 创建了一个关于 azure 缓存的示例代码,它使用Lazy<ConnectionMultiplexer> 你可以参考它或从 github 下载它

在此处输入图片说明

在此处输入图片说明


如果仍有问题,则需要检查门户上的FirewallAccess keys

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM