using (ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("redisIP:6379,allowAdmin=true"))
{
Model.SessionInstances = connection.GetEndPoints()
.Select(endpoint =>
{
var status = new Status();
var server = connection.GetServer(endpoint); // Exception thrown here!
status.IsOnline = server.IsConnected;
return status;
});
}
以上代码运行在 ASP.NET ASPX 页面的代码后面。我在运行良好的命令行程序中运行了非常相似的代码,所以我不确定我在这里做错了什么。唯一的区别是代码使用的是 foreach
循环而不是 lambda。
每次运行这段代码时,我都会得到一个异常The specified endpoint is not defined
我发现这很奇怪,因为我从同一个连接获取端点。返回的端点是正确的。
我在这里做错了什么?
我确实意识到我不应该在每次加载页面时都打开一个新连接,但这只是我不经常访问的管理页面;所以我不担心性能开销。此外,我保存的连接隐藏在 CacheClass 中,该 CacheClass 抽象出特定的提供者。
最佳答案
您遇到此错误是因为您的 lambda 表达式返回的可枚举正在被惰性计算。当您的 lambda 表达式运行时,您的连接已被 using
语句关闭。
在 using
语句中,你应该执行你的 lambda 表达式,例如通过在末尾添加 .ToList()
:
using (ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("redisIP:6379,allowAdmin=true"))
{
Model.SessionInstances = connection.GetEndPoints()
.Select(endpoint =>
{
var status = new Status();
var server = connection.GetServer(endpoint);
status.IsOnline = server.IsConnected;
return status;
}).ToList();
}
关于c# - StackExchange.ConnectionMultiplexer.GetServer 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46917437/