我有两个项目,一个是aspnet core API项目,另一个是xUnit e2e项目,都部署在两个不同的容器中。
两个容器都启动并运行后,e2e容器将尝试访问URL https://web-api-container:5010/上的Web API项目的API。但是e2e项目无法访问API项目。似乎无法使用其主机名(http://web-api-container:5010/)访问API项目。我在cli中收到以下错误:
|错误信息: System.Net.Http.HttpRequestException:连接 拒绝| ---- System.Net.Sockets.SocketException:连接被拒绝 |堆栈跟踪:在System.Net.Http.ConnectHelper.ConnectAsync(String 主机,Int32端口,CancellationToken cancelToken)|在 System.Threading.Tasks.ValueTask1.get_Result()|在 System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage 请求,CancellationToken取消令牌)|在 System.Threading.Tasks.ValueTask1.get_Result()
即使我尝试直接从主机浏览器访问Web API URL(http://web-api-container:5010/),也无法访问网站。但是,如果我从网络浏览器导航到http://localhost:5010/,则可以从API获取响应。
。/源/ WebAPI / Dockerfile:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY Source/WebAPI/WebAPI.csproj Source/WebAPI/
COPY Tests/WebAPI.UnitTests/WebAPI.UnitTests.csproj Tests/WebAPI.UnitTests/
RUN dotnet restore Tests/WebAPI.UnitTests/WebAPI.UnitTests.csproj
RUN dotnet restore Source/WebAPI/WebAPI.csproj
COPY . .
WORKDIR /src/Tests/WebAPI.UnitTests/
RUN dotnet build && dotnet test -c Release /p:CollectCoverage=true /p:CoverletOutputFormat=json /p:CoverletOutput=./TestResults/Coverage/
WORKDIR /src/Source/WebAPI
RUN dotnet build WebAPI.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish WebAPI.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebAPI.dll"]
./ Tests / WebAPI.EndToEndTests / Dockerfile:
#pull from microsoft/dotnet
FROM microsoft/dotnet:2.1.301-sdk AS build
WORKDIR /src
# copying
COPY Tests/WebAPI.EndToEndTests/WebAPI.EndToEndTests.csproj Tests/WebAPI.EndToEndTests/
#restore
RUN dotnet restore Tests/WebAPI.EndToEndTests/WebAPI.EndToEndTests.csproj
#copy rest
COPY . .
#build
WORKDIR /src/Tests/WebAPI.EndToEndTests
RUN dotnet build
CMD ["sh", "-c", "sleep 1m && dotnet test --logger trx"]
./ docker-compose.yml:
version: '3.4'
services:
webapi:
container_name: web-api-container
image: web-api-image
build:
context: .
dockerfile: Source/WebAPI/Dockerfile
ports:
- 5010:80
- 5001:443
networks:
- my-network
webapiendtoend:
container_name: e2e-test-container
image: Web-api-endtoend-test-image
build:
context: .
dockerfile: Tests/WebAPI.EndToEndTests/Dockerfile
networks:
- my-network
rabbitmq:
container_name: rabbitmq-container
image: "rabbitmq:3.7-management"
ports:
- 5672:5672
- 15672:15672
tty: true
networks:
- my-network
volumes:
- ./Source/WebAPI/RabbitMQConfig/rabbitmq.config:/etc/rabbitmq/rabbitmq.config:ro
- ./Source/WebAPI/RabbitMQConfig/definitions.json:/etc/rabbitmq/definitions.json:ro
networks:
my-network:
Tests \ WebAPI.EndToEndTests \ appsettings.json:
{
"MessageQueueApi": {
"Url": "http://web-api-container:5010/"
}
}
./ Tests / WebAPI.EndToEndTests / Test / BaseTest.cs:
public class BaseTest : IDisposable
{
public BaseTest()
{
var appSettings = AppSettings.Current;
var httpClientHandler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
};
this.HttpClient = new HttpClient(httpClientHandler)
{
BaseAddress = appSettings.MessageQueueApi.Url,
};
Console.WriteLine("<---API URL is --->" + appSettings.MessageQueueApi.Url);
Console.WriteLine("---Base address");
Console.WriteLine(this.HttpClient.BaseAddress);
}
public HttpClient HttpClient { get; set; }
public List<string> AllValues { get; set; }
public void Dispose() => this.HttpClient.Dispose();
protected async Task GetAllValues()
{
Console.WriteLine("----in get all values---");
var response = await this.HttpClient.GetAsync("api/values");
response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
protected Task<HttpResponseMessage> ClearCache() =>
this.HttpClient.PostAsJsonAsync("cache/clear", string.Empty);
}
答案 0 :(得分:1)
5010
是暴露给主机的端口。您可以使用容器内的容器端口进行通信。使用端口80
而不是使用5010
。
http://web-api-container:80
这将完成工作。