The*_*rer 3 c# asp.net redis stackexchange.redis
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;
});
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在 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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7041 次 |
| 最近记录: |