StackExchange.ConnectionMultiplexer.GetServer 不工作
StackExchange.ConnectionMultiplexer.GetServer not working
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;
});
}
以上代码 运行ning 在 ASP.NET ASPX 页面的代码后面。我在一个运行良好的命令行程序中有非常相似的代码 运行ning,所以我不确定我在这里做错了什么。唯一的区别是代码使用 foreach
循环而不是 lambda。
每次我 运行 这段代码,我都会得到一个异常 The specified endpoint is not defined
我发现这很奇怪,因为我从同一个连接获取端点。返回的端点是正确的。
我在这里做错了什么?
我意识到我不应该在每次加载页面时都打开一个新连接,但这只是我不经常访问的管理页面;所以我不担心性能开销。此外,我保存的连接隐藏在 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();
}
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;
});
}
以上代码 运行ning 在 ASP.NET ASPX 页面的代码后面。我在一个运行良好的命令行程序中有非常相似的代码 运行ning,所以我不确定我在这里做错了什么。唯一的区别是代码使用 foreach
循环而不是 lambda。
每次我 运行 这段代码,我都会得到一个异常 The specified endpoint is not defined
我发现这很奇怪,因为我从同一个连接获取端点。返回的端点是正确的。
我在这里做错了什么?
我意识到我不应该在每次加载页面时都打开一个新连接,但这只是我不经常访问的管理页面;所以我不担心性能开销。此外,我保存的连接隐藏在 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();
}