主机地址是[::]当我在docker上运行我的dotnet 9 api时

问题描述 投票:0回答:1

我有一个 .NET 9 的 API
当我通过 Kestrel 或 IIS 运行 API 目录时,一切正常,openapi/v1.json 如下所示:

  "open API": "3.0.1",
  "info": {
    "title": "Scientometrics.Api.v3 | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://localhost:7226"
    },
    {
      "url": "http://localhost:5051"
    }
  ],

但是当我在 Windows 上的 docker 桌面上运行它时,openapi 文档中的服务器 URL 是 http://[::]:8080

  "openapi": "3.0.1",
  "info": {
    "title": "Scientometrics.Api.v3 | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://[::]:8080"
    }
  ],

标量用户界面
并在通过标量或 swaggerui 请求 API 时尝试获取资源时出现网络错误
但地址为 http://localhost:8080 的 API 可以正常工作
我什至创建了一个新项目,但问题仍然存在

这是我的 docker 文件

FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Scientometrics.Api.v3/Scientometrics.Api.v3.csproj", "Scientometrics.Api.v3/"]
RUN dotnet restore "Scientometrics.Api.v3/Scientometrics.Api.v3.csproj"
COPY . .
WORKDIR "/src/Scientometrics.Api.v3"
RUN dotnet build "Scientometrics.Api.v3.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Scientometrics.Api.v3.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Scientometrics.Api.v3.dll"]
asp.net-core openapi scalar
1个回答
0
投票

我通过覆盖 OpenAPI 文档中的服务器解决了这个问题:

app.MapScalarApiReference(options =>
{
    options.Servers =
    [
        new ScalarServer("http://localhost:8080")
    ];
});

我从这篇文章中得到了这个想法:在此处输入链接描述

© www.soinside.com 2019 - 2024. All rights reserved.