最近在使用.Net SignalR Reids時一直出現無法連接的情況,所以記錄一下遇到的坑。
因爲用到的是Azure Reids服務器,開啓了SSL(6380端口),同時禁用了TLS1.1和TLS1.2,
正常如果不使用SSL(6380端口)的話可以直接連接就可以了,但是如果是SSL就需要做一些修改纔可以連接。
SignalR 需要Nuget引用如下包(使用StackExchangeRedis):
同時Startup中連接代碼如下(ssl和sslprotocols爲關鍵參數,用於連接開啓ssl的redis服務器):
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
//使用Redis方式進行連接,用於負載均衡場景
var connString = $"xxxxx:6380,password=xxxx,ssl=true,abortConnect=false,sslprotocols=tls12";
GlobalHost.DependencyResolver.UseStackExchangeRedis(new RedisScaleoutConfiguration(connString, "ChatHub"));
}
}
另外如果使用ServiceStack.Redis工具包的話需要做如下調整(password需要進行url編碼):
public IRedisClient GetClient()
{
var password = HttpUtility.UrlEncode("xxxxx");
var connString = $"redis://xxxx:6380?ssl=true&sslprotocols=Tls12&password={password}";
var redisManager = new RedisManagerPool(connString);
return redisManager.GetClient();
}
附ServiceStack.Redis和StackExchangeRedis使用說明連接:
https://github.com/ServiceStack/ServiceStack.Redis#servicestackredis-ssl-support