我目前正在尝试在docker容器中运行我的pact.net示例测试,但是我得到了以下错误:
Starting test execution, please wait...
pact-consumer-tests | [xUnit.net 00:00:19.9436627]
pact_consumer.tests.AddConsumerTest.Add_customer [FAIL]
pact-consumer-tests | Failed
pact_consumer.tests.AddConsumerTest.Add_customer
pact-consumer-tests | Error Message:
pact-consumer-tests | System.AggregateException : One or more errors
occurred. (One or more errors occurred. (Cannot assign requested
address)) (The following constructor parameters did not have matching
fixture data: ConsumerApiPact data)
pact-consumer-tests | ---- System.AggregateException : One or more
errors occurred. (Cannot assign requested address)
pact-consumer-tests | -------- System.Net.Http.HttpRequestException :
Cannot assign requested address
pact-consumer-tests | ------------ System.Net.Sockets.SocketException
: Cannot assign requested address
pact-consumer-tests | ---- The following constructor parameters did
not have matching fixture data: ConsumerApiPact data
pact-consumer-tests | Stack Trace:
pact-consumer-tests |
pact-consumer-tests | ----- Inner Stack Trace #1
(System.AggregateException) -----
pact-consumer-tests | at
PactNet.Mocks.MockHttpService.Host.RubyHttpHost.Start() in
C:\projects\pact-
net\PactNet\Mocks\MockHttpService\Host\RubyHttpHost.cs:line 65
pact-consumer-tests | at PactNet.PactBuilder.MockService(Int32
port, JsonSerializerSettings jsonSerializerSettings, Boolean enableSsl,
IPAddress host) in C:\projects\pact-net\PactNet\PactBuilder.cs:line 82
pact-consumer-tests | at
pact_consumer.tests.context.ConsumerApiPact..ctor() in /app/pact-
consumer.tests/context/ConsumerApiPact.cs:line 27
pact-consumer-tests | ----- Inner Stack Trace -----
pact-consumer-tests | at
System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port,
CancellationToken cancellationToken)
....任何我发送到主机IPAddress.Any和IpAddress.Loopback的框架,但我似乎仍然得到这个问题?
我是不是遗漏了什么?它在docker容器中运行,没有其他服务在运行。
我可以使用dotnet test在OSX上运行它,一切都很好。
非常感谢你提前
理查德
发布于 2018-07-12 18:00:20
System.Net.Sockets.SocketException : Cannot assign requested address看起来确实像是系统无法绑定到端口--你确定该端口在主机上可用吗(不仅仅是Docker容器)?
发布于 2020-03-06 03:55:08
在c#中,我也遇到过同样的错误。同样的技术也可以用在ruby问题上。我研究了在docker容器中运行的dotnet应用程序的问题。快速的答案是,它可能正在尝试连接到某个东西,但没有任何东西在侦听,或者它无法到达它,因此您需要在docker行中启用一些网络挂钩。对于这个答案的其余部分,我将说明我是如何得出这个结论的。我正在运行从Github:https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/02.echo-bot下载的EchoBot
首先,我们将其构建到一个容器中,并部署它,然后尝试从机器人仿真器中使用它来重现错误。模拟器在我的桌面上运行,并通过端口3978连接到机器人。
启动一个error机器人容器,并从仿真器中使用它来查看原始错误:
$ docker run --rm -it --entrypoint=bash -p 3978:3978 myImage
# dotnet EchoBot1.dll
...
info: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
Sending activity. ReplyToId: adfa9af0-5f17-11ea-8765-033688f22eac
fail: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
[OnTurnError] unhandled error : Cannot assign requested address
System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---您可以看到,存在一个套接字异常,其中包含cannot assign socket错误。我们可以从等式中删除docker,以查看错误是否发生变化。
要查看错误,请从图片中移除docker
$ dotnet run
...
info: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
Sending activity. ReplyToId: 3a427871-5f18-11ea-8765-033688f22eac
fail: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
[OnTurnError] unhandled error : Connection refused
System.Net.Http.HttpRequestException: Connection refused ---> System.Net.Sockets.SocketException: Connection refused
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---您可以看到,当相同的代码在docker之外运行时,该错误实际上是连接被拒绝。那么我们可以做些什么来查看它连接到哪里呢?如果你在linux上,你可以在windows上运行tcpdump或wireshark。
捕获网络数据包,查看是否存在出站连接
$tcpdump -ilo -n -v | tee tcpdump-output
...
19:38:45.336179 IP (tos 0x0, ttl 64, id 12504, offset 0, flags [DF], proto TCP (6), length 60)
127.0.0.1.46215 > 127.0.0.1.52148: Flags [S], cksum 0xfe30 (incorrect -> 0xea5f), seq 549373294, win 65495, options [mss 65495,sackOK,TS val 3769835141 ecr 0,nop,wscale 7], length 0这里面会有很多垃圾信息,所以我将输出结果保存到一个文件中,然后使用编辑器删除那些让人分心的代码行(比如DNS查询),但是您可以看到有些东西正试图连接到端口52148。
原来本地仿真器正在侦听同一端口,因此我需要将该端口连接到运行仿真器的台式机。有很多方法可以做到这一点,但我更喜欢旧的SSH方法,它是方便的。
连接侦听隧道
$ ssh nmishr@104.xxx.xxx.xxx -R 52148:localhost:52148一旦我在我的本地主机上挂接了监听程序,我们需要启用docker容器来与主机对话,错误就会消失:
使用--=主机运行docker
$ docker run --rm -it --entrypoint=bash --network=host myImage
# dotnet EchoBot1.dll
...
info: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
Sending activity. ReplyToId: c321ebb1-5f1a-11ea-8765-033688f22eac
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action method EchoBot1.Controllers.BotController.PostAsync (EchoBot1), returned result Microsoft.AspNetCore.Mvc.EmptyResult in 518.6485ms.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action EchoBot1.Controllers.BotController.PostAsync (EchoBot1) in 550.6136ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 687.9129ms 200https://stackoverflow.com/questions/51099626
复制相似问题