Dotnet 6项目Redis连接失败问题求助
Redis连接失败问题排查与修复
问题场景
在Dotnet 6项目中集成StackExchange.Redis后,配置完成后无法连接Redis,抛出错误:
StackExchange.Redis.RedisConnectionException: 'It was not possible to connect to the redis server(s).'
当前Redis缓存服务配置代码:
// Add Redis cache service builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = "localhost:44398"; });
使用IIS Express启动项目,对应的launchSettings.json配置:
{ "$schema": "https://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:65528", "sslPort": 44398 } }, "profiles": { "RT.Api": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "https://localhost:7299;http://localhost:5299", "dotnetRunMessages": true }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }
错误原因
你把**IIS Express的SSL端口(44398)**当成了Redis服务的运行端口,这是核心错误:
- IIS Express的sslPort是用来承载Asp.Net Core Web应用的HTTPS端口,和Redis服务完全无关。
- Redis默认运行端口是6379,除非你手动修改过Redis的配置文件来变更端口。
修复步骤
确认Redis实际运行端口
- 打开Redis的配置文件(通常是
redis.conf或redis.windows.conf),查找port配置项,确认Redis服务监听的端口,默认是6379。 - 或通过命令行查看进程监听端口:Windows用
netstat -ano | findstr "redis",Linux用netstat -tulpn | grep redis。
- 打开Redis的配置文件(通常是
修正Redis连接配置
将Redis配置中的端口替换为正确的Redis服务端口,默认端口示例:builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = "localhost:6379"; });额外检查项
- 确保Redis服务已启动(Windows服务中查看状态,或命令行执行
redis-server启动)。 - 确认本地防火墙未阻止Redis端口的连接请求。
- 如果Redis设置了密码,需在连接字符串中添加参数,例如
localhost:6379,password=yourRedisPassword。
- 确保Redis服务已启动(Windows服务中查看状态,或命令行执行
内容的提问来源于stack exchange,提问作者Hamid
相关产品推荐
相关产品推荐

