Skip to content

Commit 605b1db

Browse files
authored
Merge pull request #139 from cnblogs/hotfix/138-PlatformNotSupportedException
Workaround for PlatformNotSupportedException on linux with .net core 2.1
2 parents 97b5dc3 + 87efa8c commit 605b1db

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

Enyim.Caching/Memcached/PooledSocket.cs

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ void Cancel()
6565
}
6666
cts.Token.Register(Cancel);
6767

68-
_socket.Connect(_endpoint);
68+
try
69+
{
70+
_socket.Connect(_endpoint);
71+
}
72+
catch (PlatformNotSupportedException)
73+
{
74+
var ep = GetIPEndPoint(_endpoint);
75+
_socket.Connect(ep.Address, ep.Port);
76+
}
6977

7078
if (_socket != null)
7179
{
@@ -94,20 +102,28 @@ public async Task ConnectAsync()
94102
{
95103
bool success = false;
96104

97-
var connTask = _socket.ConnectAsync(_endpoint);
98-
99-
if (await Task.WhenAny(connTask, Task.Delay(_connectionTimeout)) == connTask)
100-
{
101-
await connTask;
102-
}
103-
else
105+
try
104106
{
105-
if (_socket != null)
107+
var connTask = _socket.ConnectAsync(_endpoint);
108+
109+
if (await Task.WhenAny(connTask, Task.Delay(_connectionTimeout)) == connTask)
106110
{
107-
_socket.Dispose();
108-
_socket = null;
111+
await connTask;
109112
}
110-
throw new TimeoutException($"Timeout to connect to {_endpoint}.");
113+
else
114+
{
115+
if (_socket != null)
116+
{
117+
_socket.Dispose();
118+
_socket = null;
119+
}
120+
throw new TimeoutException($"Timeout to connect to {_endpoint}.");
121+
}
122+
}
123+
catch (PlatformNotSupportedException)
124+
{
125+
var ep = GetIPEndPoint(_endpoint);
126+
await _socket.ConnectAsync(ep.Address, ep.Port);
111127
}
112128

113129
if (_socket != null)
@@ -423,6 +439,27 @@ public async Task WriteAsync(IList<ArraySegment<byte>> buffers)
423439
throw;
424440
}
425441
}
442+
443+
private IPEndPoint GetIPEndPoint(EndPoint endpoint)
444+
{
445+
if (endpoint is DnsEndPoint)
446+
{
447+
var dnsEndPoint = (DnsEndPoint)endpoint;
448+
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
449+
ip.AddressFamily == AddressFamily.InterNetwork);
450+
if (address == null)
451+
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", endpoint));
452+
return new IPEndPoint(address, dnsEndPoint.Port);
453+
}
454+
else if (endpoint is IPEndPoint)
455+
{
456+
return endpoint as IPEndPoint;
457+
}
458+
else
459+
{
460+
throw new Exception("Not supported EndPoint type");
461+
}
462+
}
426463
}
427464
}
428465

0 commit comments

Comments
 (0)