Skip to content

Commit eba2ea8

Browse files
committed
Support IP address connection string in Basket.API
Dns.GetHostAddressesAsync can return problematic results when passed an IP address, and if the connection string is already an IP address, we needn't call it anyway.
1 parent 2f3df27 commit eba2ea8

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,21 @@ private async Task<IServer> GetServer()
9494

9595
private async Task ConnectToRedisAsync()
9696
{
97-
//TODO: Need to make this more robust. Also want to understand why the static connection method cannot accept dns names.
98-
var ips = await Dns.GetHostAddressesAsync(_settings.ConnectionString);
99-
_logger.LogInformation($"Connecting to database {_settings.ConnectionString} at IP {ips.First().ToString()}");
100-
_redis = await ConnectionMultiplexer.ConnectAsync(ips.First().ToString());
97+
// TODO: Need to make this more robust. ConnectionMultiplexer.ConnectAsync doesn't like domain names or IPv6 addresses.
98+
if (IPAddress.TryParse(_settings.ConnectionString, out var ip))
99+
{
100+
_redis = await ConnectionMultiplexer.ConnectAsync(ip.ToString());
101+
_logger.LogInformation($"Connecting to database at {_settings.ConnectionString}");
102+
}
103+
else
104+
{
105+
// workaround for https://github.com/StackExchange/StackExchange.Redis/issues/410
106+
var ips = await Dns.GetHostAddressesAsync(_settings.ConnectionString);
107+
_logger.LogInformation($"Connecting to database {_settings.ConnectionString} at IP {ips.First().ToString()}");
108+
_redis = await ConnectionMultiplexer.ConnectAsync(ips.First().ToString());
109+
}
101110
}
102-
103-
111+
104112
}
105113
}
106114

0 commit comments

Comments
 (0)