【问题标题】:Catch Microsoft.Extensions.Caching.Redis connection fail?赶上 Microsoft.Extensions.Caching.Redis 连接失败?
【发布时间】:2017-10-27 05:49:38
【问题描述】:

我需要在我的 asp.net 核心项目中使用 Microsoft.Extensions.Caching.Redis。

我把这段代码放到 ConfigureService (Startup.cs) 中:

IDistributedCache cache = new RedisCache(new RedisCacheOptions
{
    Configuration = Configuration.GetConnectionString("Redis"),
    InstanceName = "Master"
});

services.AddSingleton<IDistributedCache>(cache);

我需要的是捕获连接异常(以防 Redis 服务器关闭,或者服务器无法访问),但我还没有找到检查连接的方法以及是否将代码包装到 try/catch什么都没有发生。

有没有办法完成我的任务?

谢谢

【问题讨论】:

    标签: c# asp.net caching redis asp.net-core


    【解决方案1】:

    在这一步你还没有打开到 Redis 的连接,你只是创建了一个 RedisCache 类的实例。

    稍后将在您使用类的公共方法时打开连接。以RedisCache.GetAsync 方法为例:

    // IDistributedCache  _cache;
    // ...
    // This is the place where you may get the exception
    var value = await _cache.GetAsync(key);
    

    这里将使用私有Connect 方法在内部(如果需要)创建连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-23
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多