Jac*_*rdy 4 .net swagger docker
使用 Docker 运行 .Net Core API (v2.2) 时,无法找到 .XML 文件以进行 swagger 并尝试在本地主机上访问它时崩溃,并出现以下错误:
FileNotFoundException: Could not find file '/app/Account.API.XML'.
在检查容器时,我可以看到该文件存在于目录中,但我不确定为什么它无法检测到它。这是我的容器的内容:
root@538b7c4e4b45:/app# ls
Account.API.deps.json Account.API.pdb Account.API.xml Swashbuckle.AspNetCore.Swagger.dll Swashbuckle.AspNetCore.SwaggerUI.dll appsettings.Development.json web.config Account.API.dll Account.API.runtimeconfig.json Microsoft.OpenApi.dll Swashbuckle.AspNetCore.SwaggerGen.dll Swashbuckle.AspNetCore.dll appsettings.json
Run Code Online (Sandbox Code Playgroud)
这是我的 Dockerfile 的内容:
# Create a release executable of the service
FROM microsoft/dotnet:2.2-sdk AS builder
WORKDIR /build
COPY ./ ./
RUN dotnet publish Account.API/Account.API.csproj --framework netcoreapp2.2 --configuration Release --output /release
# Run the executable with .Net Core runtime
FROM microsoft/dotnet:2.2-aspnetcore-runtime
ARG ENV_NAME
ENV ASPNETCORE_ENVIRONMENT=$ENV_NAME
WORKDIR /app
COPY --from=builder /release ./
EXPOSE 80
ENTRYPOINT ["dotnet", "Account.API.dll"]
Run Code Online (Sandbox Code Playgroud)
文件注册如下:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Account API", Version = "v1" });
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
Run Code Online (Sandbox Code Playgroud)
但是,在 Docker 之外,如果我在 Dockerfile 中创建具有相同选项的版本,然后在本地运行 .dll,它能够找到所需的 .XML 文件并按预期运行。
关于为什么会这样的任何想法?任何想法将不胜感激。
谢谢
Jac*_*rdy 10
解决-作为泊坞窗容器在Linux上运行,和文件是不区分大小写,我不得不Program.cs中以下行改变:
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML"以var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"小写“XML”。
| 归档时间: |
|
| 查看次数: |
1793 次 |
| 最近记录: |