C# web api不会在azure上的linux容器中使用NetCore 3 web api启动
我正在尝试对.NETCore3进行“dockerize”。我使用作为我的proff of concept应用程序的基础。这是一个.net core 3 webapi项目,前端为Angular客户端应用程序 我所拥有的:C# web api不会在azure上的linux容器中使用NetCore 3 web api启动,c#,azure,docker,asp.net-core,azure-devops,C#,Azure,Docker,Asp.net Core,Azure Devops,我正在尝试对.NETCore3进行“dockerize”。我使用作为我的proff of concept应用程序的基础。这是一个.net core 3 webapi项目,前端为Angular客户端应用程序 我所拥有的: 拉取请求的基代码在本地工作 我必须克服的一个初始问题是在本地非开发环境中为identity server 4设置证书,我必须装载一个带有证书的卷,并从appsettings.json文件中引用它,如 我在azure中设置了一个CI/CD管道来构建项目,并将映像部署到azure容器
services:
webui:
image: ${DOCKER_REGISTRY-}webui
build:
context: .
dockerfile: src/WebUI/Dockerfile
environment:
- "UseInMemoryDatabase=false"
- "ASPNETCORE_ENVIRONMENT=Production"
- "ConnectionStrings__DefaultConnection=myconnection"
- "ASPNETCORE_Kestrel__Certificates__Default__Password=mypass"
- "ASPNETCORE_Kestrel__Certificates__Default__Path=/security/mycert.pfx"
ports:
- "5000:5000"
- "5001:5001"
volumes:
- mcpdata:"/security:/"
restart: always
mcpdata是装载并包含实际证书的azure文件共享的名称
以下是CI/CD的my azure-pipeline.yml:
trigger:
- staging
resources:
- repo: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: '****'
imageRepository: 'cleanarchitecture'
containerRegistry: '****.azurecr.io'
dockerComposeFilePath: '$(Build.SourcesDirectory)docker-compose.Production.yml'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerComposeFile: $(dockerComposeFilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: staging
问题?
有人能帮我弄清楚为什么我的web api没有运行,但没有抛出错误。至少,如果有人能帮我查看docker日志中的错误,我会很高兴
提前感谢我尝试重复,使用“干净的体系结构”时使用了以下内容(注意,我在MacOS上使用的是zsh,但在Windows/Linux上也同样适用):
takeclean\u架构
dotnet新建--安装Clean.Architecture.Solution.Template
dotnet新ca sln
文档建议,在Visual Studio中单击F5将启动模板,尽管我必须执行以下操作:
cd src/WebUI/ClientApp
npm安装
此时,应用程序通过点击F5本地启动。注意,这里发生的事情是ASP.NETCore将请求转发到开发服务器,这样做非常有效,它提供了ng服务--端口53543
,并在端口5001上启动ASP.NETCore(在我的例子中是Kestrel),浏览到http://127.0.0.1:53543
直接提供角度页面。浏览至https://localhost:5001
显示ASPNetCore转发给angular的同一个angular页面。一切都很混乱。。。更详细
注意在Startup.cs中存在以下代码行,这些代码行通常基于环境变量ASPNETCORE\u environment
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
-- and within "app.UseSpa"
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
无论如何,看起来您已经将环境变量设置为Production,它应该只服务于ClientApp\dist
文件夹中的构建文件(而不是转发到dev服务器),这表明如果您看到Angular,那么.Net核心服务正在运行。。。我会先尝试重建Dockerfiles
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
ENV ASPNETCORE_URLS=https://+:5001;http://+:5000
WORKDIR /app
EXPOSE 5000
EXPOSE 5001
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt install -y nodejs
WORKDIR /src
COPY ./src/WebUI/WebUI.csproj src/WebUI/
COPY ./src/Application/Application.csproj src/Application/
COPY ./src/Domain/Domain.csproj src/Domain/
COPY ./src/Infrastructure/Infrastructure.csproj src/Infrastructure/
RUN dotnet restore "src/WebUI/WebUI.csproj"
COPY . .
WORKDIR "/src/src/WebUI"
RUN dotnet build "WebUI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebUI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CleanArchitecture.WebUI.dll"]
然后按如下方式构建和运行:
"IdentityServer": {
"Key": {
"Type": "File",
"FilePath": "/app/https/https/clean-arch.pfx",
"Password": "anything"
}
}
#构建需要一段时间
docker build-f./src/WebUI/Dockerfile-t clean架构。
#注意,这是第一次失败,因为我设置为clean_架构,所以入口点不正确
docker run--rm-it-P5000:5000-P5001:5001清洁体系结构
#运行容器并覆盖入口点
docker运行--rm-it--entrypoint/bin/bash-clean体系结构
#从容器内部。。。
root@93afb0ad21c5:/app#dotnet clean_architecture.WebUI.dll
#注意,在.Net 3.1中,您也可以直接执行此操作,如下所示:
root@93afb0ad21c5:/app#./clean_architecture.WebUI
现在LocalDB有一个问题:System.PlatformNotSupportedException:此平台不支持LocalDB。
将appsettings.Production.json切换为“UseInMemoryDatabase”:true
然后问题似乎是证书
我使用以下方法创建了一个证书:
dotnet dev certs https-ep./https/clean-arch.pfx-p任何内容
对于IdentityServer,我将appSettings.Production.json更改如下:
"IdentityServer": {
"Key": {
"Type": "File",
"FilePath": "/app/https/https/clean-arch.pfx",
"Password": "anything"
}
}
然后在Linux上运行,可能意味着运行Kestrel,这意味着我们也需要在那里提供HTTPS证书,我在Program.cs中设置了以下内容
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.ConfigureKestrel((上下文,选项)=>
{
options.AllowSynchronousIO=true;
options.Listen(IPAddress.Loopback,5000,listenOptions=>
{
UseConnectionLogging();
version: '3.4'
services:
webui:
environment:
- "ASPNETCORE_ENVIRONMENT=Development"
- "SpaBaseUrl=http://clientapp:4200"
clientapp:
image: ${DOCKER_REGISTRY-}clientapp
build:
context: src/WebUI/ClientApp
dockerfile: Dockerfile
depends_on:
- webui
restart: on-failure
db:
ports:
- "1433:1433"