gpt4 book ai didi

c# - API 不起作用,使用 Docker 和 ASP.NET CORE

转载 作者:行者123 更新时间:2023-12-02 20:56:41 32 4
gpt4 key购买 nike

我制作了一个 ASP.NET Core Web API,它可以 ping 一个 Mongodb 和一个 docker 容器。 Web api 调用在不使用 docker 时工作,但在我使用它时返回 Not Found 错误。我认为这与我正在运行的端口有关,但我不确定因为这是我第一次使用 docker。
在命令提示符下,我这样做:docker build -t projectname .然后我运行图像docker run -p 8080:80 projectname运行后,如果我调用 http://localhost:8080/api/classname,我会收到 Not Found 错误消息“这是我的无效消息”,所以看起来我的 docker 容器正在运行。当我调用 http://localhost:8080/api/classname?id=1(这是一个有效的 id)时,我仍然收到 Not Found 错误,但没有收到“这是我的无效消息”文本.
类名 Controller API 获取:

[Route("api/[controller]")]
[ApiController]
public class ClassNameController : Controller
{
[HttpGet]
public IActionResult GetDescendents(int id)
{
var mongoDbService = new MongoDbService();


try
{
var node = mongoDbService.GetTreeNodeById(id);

if (node == null)
{
return NotFound("this is my invalid message");
}
else
{
//stuff happens
}
}
catch (Exception e)
{
return BadRequest(e);
}

return Ok();
}
Dockerfile:
See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ProjectName/ProjectName.csproj", "ProjectName/"]
RUN dotnet restore "ProjectName/ProjectName.csproj"
COPY . .
WORKDIR "/src/ProjectName"
RUN dotnet build "ProjectName.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProjectName.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.dll"]
启动设置.json:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:8080",
"sslPort": 8080
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"DockerAPI": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/",
"publishAllPorts": true,
"useSSL": true
}
}
}

最佳答案

考虑到您尚未定义它,当您在 docker 容器中使用 Web API 和 mongo 服务时,它可能位于不同的网络上。
解决此问题的一种快速方法是更改​​ localhost来自你的 mongo 连接字符串,来自你的 IP 号码,在你的 appsettings 中, 像这样:

"ConnectionString":"mongodb://192.168.0.27:27017"
请注意,在您的 Dockerfile 中您没有定义环境,在这种情况下,您在 docker 中的应用程序将使用生产 appsettings而不是你的 appsettings.Development .
如果您在 compose 中更喜欢两者,您可以为所有服务定义一个网络,然后在连接字符串上只使用服务的名称,如下所示:
 "ConnectionString":"mongodb://mongo"
示例 docker-compose.yml :
version: "3.7"

services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
networks:
- mynetwork

mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
networks:
- mynetwork

webapi:
container_name: webapi
build:
context: .
dockerfile: ./YourProject/Dockerfile
ports:
- 8080:80
depends_on:
- mongo
links:
- mongo
networks:
- mynetwork

networks:
mynetwork:
driver: bridge
进而:
docker-compose up

关于c# - API 不起作用,使用 Docker 和 ASP.NET CORE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62561312/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com