我正在本地 PC 上使用 Service Fabric Mesh,但我在 (Windows) 容器内访问 Internet 时遇到了困难。
我正在使用 Visual Studio 2017 中的标准 Web 应用程序模板:
public static async Task<int> Main(string[] args)
{
try
{
var test = await (new System.Net.Http.HttpClient()).GetStringAsync("http://google.com");
Console.WriteLine(test);
您可能希望打印出一些 HTML,但我得到了这个异常:
System.Net.Http.HttpRequestException:不知道这样的主机---> System.Net.Sockets.SocketException:在 System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32 端口,CancellationToken cancelToken)处不知道这样的主机--- 内部异常堆栈跟踪结束 --- 在 System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancelToken) 在 System.Threading.Tasks.ValueTask`1.get_Result() 在 System.Net .Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancelToken) 在 System.Threading.Tasks.ValueTask`1.get_Result() 在 System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask) 在 System.Threading.Tasks。 System.Net.Http.HttpConnectionPool 中的 ValueTask`1.get_Result()。在 System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage 请求, CancellationTokenSource cts, System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancelToken)布尔 disposeCts) 在 System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask) 在 Web1.Program.Main(String[] args)Web1.Program.Main(String[] args) 处的 Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)Web1.Program.Main(String[] args) 处的 Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
使用的基本 docker 映像是microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1709
,如果我运行:
docker run microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1709 ping google.com
然后我得到:
PS C:\Windows\system32> docker run microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1709 ping google.com
Pinging google.com [172.217.168.238] with 32 bytes of data:
Reply from 172.217.168.238: bytes=32 time=16ms TTL=56
Reply from 172.217.168.238: bytes=32 time=15ms TTL=56
Reply from 172.217.168.238: bytes=32 time=15ms TTL=56
Reply from 172.217.168.238: bytes=32 time=16ms TTL=56
Ping statistics for 172.217.168.238:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 15ms, Maximum = 16ms, Average = 15ms
我究竟做错了什么?
更新1:
当我直接运行 Docker 映像时,我得到:
PS C:\Users\XXX> docker run microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1803 ipconfig.exe /all
Windows IP Configuration
Host Name . . . . . . . . . . . . : XXX
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : XXX
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : XXX
Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
Physical Address. . . . . . . . . : 00-15-5D-38-EE-09
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::1c14:1ec3:9b85:5f56%4(Preferred)
IPv4 Address. . . . . . . . . . . : 172.20.192.4(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . : 172.20.192.1
DHCPv6 IAID . . . . . . . . . . . : 67114333
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-23-7C-13-F1-00-15-5D-38-EE-09
DNS Servers . . . . . . . . . . . : 172.20.192.1
10.10.0.184 <-- IP of my local machine
10.10.0.1 <-- My router
NetBIOS over Tcpip. . . . . . . . : Disabled
如果我System.Diagnostics.Process.Start("ipconfig.exe", "/all");
在 Service Fabric 中运行的代码中执行相同的 ( ):
Windows IP Configuration
Host Name . . . . . . . . . . . . : 0ded1f75fa51
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : Application1
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : XXX
Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
Physical Address. . . . . . . . . : 00-15-5D-38-E5-2C
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::1d25:4204:2cd4:1bb0%4(Preferred)
IPv4 Address. . . . . . . . . . . : 172.20.202.176(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . : 172.20.192.1
DHCPv6 IAID . . . . . . . . . . . : 67114333
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-23-7C-11-32-00-15-5D-38-E5-2C
DNS Servers . . . . . . . . . . . : 172.20.192.1
10.10.0.184
NetBIOS over Tcpip. . . . . . . . : Disabled
如您所见,10.10.0.1(我的路由器/网关在上次转储中作为 DNS 服务器丢失)。我该如何添加?