简体   繁体   English

如何将 aws redis 与 c# 连接

[英]how to connection aws redis with c#

how to use c# to connection aws redis?如何使用 c# 连接 aws redis?

I create aws redis done and create linux host to conntection redis is successed,我创建 aws redis 完成并创建 linux 主机以连接 redis 成功,

but when I use c# code to connection, I get failed.但是当我使用 c# 代码进行连接时,我失败了。

private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect("test.qzgpeh.ng.0001.use2.cache.amazonaws.com:6379,abortConnect=false");
    });

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return lazyConnection.Value;
        }
    }

Error Message错误信息

No connection is active/available to service this operation: GET hello;没有连接处于活动状态/可用于服务此操作:GET hello; UnableToConnect on test.qzgpeh.ng.0001.use2.cache.amazonaws.com:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 5s ago, last-write: 5s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 5s ago, v: 2.1.58.34321, mc: 1/1/0, mgr: 10 of 10 available, clientName: HAUSER, IOCP: (Busy=0,Free=1000,Min=12,Max=1000), WORKER: (Busy=0,Free=32767,Min=12,Max=32767), v: 2.1.58.34321 UnableToConnect on test.qzgpeh.ng.0001.use2.cache.amazonaws.com:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, 未完成: 0, last-read: 5s ago, last-write: 5s前,保持活动状态:60 秒,state:正在连接,管理器:10 个中的 10 个可用,最后一次心跳:从不,全局:5 秒前,v:2.1.58.34321,mc:1/1/0,管理器:10 个可用, 客户端名称: HAUSER, IOCP: (Busy=0,Free=1000,Min=12,Max=1000), WORKER: (Busy=0,Free=32767,Min=12,Max=32767), v: 2.1.58.34321

how I can to fix it?我该如何解决?

Posting if someone facing a similar issue.如果有人遇到类似问题,请发布。

I was trying to access Redis data from the Elastic Beanstalk.Net application and facing a similar issue.我试图从 Elastic Beanstalk.Net 应用程序访问 Redis 数据并面临类似问题。

In my case, it was an issue from a security group.就我而言,这是来自安全组的问题。 I followed the below steps我按照以下步骤

  1. Create my VPC and Subnet and Security Group创建我的 VPC 和子网和安全组
  2. Create and deploy an application in EBS.在 EBS 中创建和部署应用程序。
  3. Create a security group (for Redis) and allow connection from EBS Security group创建一个安全组(用于 Redis)并允许来自 EBS 安全组的连接
  4. Create Redis in same VPC and Security group (step 3 security group)在同一 VPC 和安全组中创建 Redis(步骤 3 安全组)
  5. Able to access Redis from.Net application能够从.Net应用访问Redis

Redis Security Group Inbound Rule Redis 安全组入站规则

在此处输入图像描述

.NET Core Sample Code .NET 内核示例代码

public class PrivacyModel : PageModel
{
    private readonly ILogger<PrivacyModel> _logger;
    private readonly IDatabase cache;
    private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect("******-redis-cache.yq8meb.0001.aps1.cache.amazonaws.com:6379,abortConnect=false");
    });

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return lazyConnection.Value;
        }
    }

    public PrivacyModel(ILogger<PrivacyModel> logger)
    {
        _logger = logger;
        this.cache = Connection.GetDatabase();
    }

    [BindProperty]
    public string StartTime { get; set; }
    public void OnGet()
    {
        StartTime = this.cache.StringGet("StartTime");
        if (string.IsNullOrWhiteSpace(StartTime))
        {
            StartTime = DateTime.Now.ToString();
            this.cache.StringSet("StartTime", StartTime);
        }
    }
}

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

相关问题 无法在 .net C# 中连接 AWS Redis 缓存 - RedisConnectionException: &#39;没有连接处于活动状态/可用于服务此操作 - Unable to connect AWS Redis cache in .net C# - RedisConnectionException: 'No connection is active/available to service this operation 使用C#的AWS Elastic Cache Redis - AWS Elastic Cache Redis with C# C#Winforms与MySQL AWS数据库的连接 - C# Winforms connection to MySQL AWS Database 为什么Redis连接可以在C#中工作而无需指定凭据? - Why Redis connection works from C# without specifying credentials? 如何在 c# 中使用 redis 管道(StackExchange.Redis)? - How to use redis pipiline(StackExchange.Redis) in c#? 如何在C#中更新Redis列表 - how to update redis list in C# 如何使用C#连接到Redis服务器? - How to connect to Redis server using C#? 如何将Redis ArrayRedisResult转换为C#数组? - How to convert a Redis ArrayRedisResult into a C# array? 如何使用 C# Rx 实现 Redis 流 - How to implement Redis streams with C# Rx 为什么错误 No connection is available to service this operation: SETEX while write to Redis Cache in C# console application 以及如何解决它 - Why error No connection is available to service this operation: SETEX while writing to Redis Cache in C# console application and how to solve it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM