更新StackExchange.Redis至2.6.96后出现编译错误CS1545
解决StackExchange.Redis 2.6.96版本的CS1545编译错误
出现这个错误是因为StackExchange.Redis 2.6.x版本对ConfigurationOptions.EndPoints的实现做了调整,导致直接用属性访问语法(config.EndPoints)无法被当前项目的C#编译器识别。以下是几种可行的解决办法:
方法1:直接调用访问器方法(按错误提示操作)
如果需要临时兼容现有代码结构,可以直接调用错误提示里的访问器方法:
- 获取端点集合:
var config = new ConfigurationOptions(); var endPoints = config.get_EndPoints(); - 设置端点集合:
var newEndPoints = new EndPointCollection { new DnsEndPoint("localhost", 6379) }; config.set_EndPoints(newEndPoints);
方法2:使用官方推荐的AddEndPoint系列方法
StackExchange.Redis官方更推荐通过专门的方法来管理端点,这种方式更符合新版本的设计逻辑,也能避免类似的语法问题:
var config = new ConfigurationOptions(); // 添加单个端点(IP+端口) config.AddEndPoint("127.0.0.1", 6379); // 添加单个端点(域名+端口字符串) config.AddEndPoint("redis.example.com:6379"); // 批量添加多个端点 config.AddEndPoints("redis1:6379", "redis2:6380", "redis3:6381");
方法3:升级项目的C#语言版本
如果你的项目还在使用较旧的C#版本(比如C# 7.0及以下),升级到C# 8.0或更高版本后,编译器就能正常识别ConfigurationOptions.EndPoints属性的访问语法了。具体操作:
- 右键项目 → 属性 → 生成 → 高级 → 语言版本,选择较新的版本(比如
C# 8.0或最新版本)
内容的提问来源于stack exchange,提问作者Pandurang Shinde
相关产品推荐
相关产品推荐

