【问题标题】:Unable to start .NET Core 3.1 Worker Service on Docker using Serilog无法使用 Serilog 在 Docker 上启动 .NET Core 3.1 Worker 服务
【发布时间】:2020-11-23 19:25:47
【问题描述】:

我正在使用 Visual Studio 2019(已启用 Docker 支持)编写一个 .Net Core 3.1 Worker Service 应用程序。

如果我使用 Docker 启动应用程序一切正常,但是当我向项目添加 Serilog.AspNetCore 3.4.0 依赖项时,我无法再使用 docker 进行调试了。

案例:

  • 在没有 Serilog 的情况下在本地启动应用程序 --> 确定
  • 在没有 Serilog 的情况下在 Docker 上启动应用程序 --> 确定
  • 使用 Serilog 在本地启动应用程序 --> 确定
  • 使用 Serilog 在 Docker 上启动应用程序 --> 错误

Visual Studio 返回的错误是:

这是控制台返回的错误:

-------------------------------------------------------------------
You may only use the Microsoft .NET Core Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software
to help you develop and test your applications.
-------------------------------------------------------------------
It was not possible to find any compatible framework version
The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
  - No frameworks were found.

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=3.1.0&arch=x64&rid=debian.10-x64
The program 'dotnet' has exited with code 150 (0x96).

编辑:

这是VS 2019自动生成的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/runtime:3.1-buster-slim AS base
WORKDIR /app

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MensaNotificationService.dll"]

问题是什么,我该如何解决?

【问题讨论】:

  • 您是否尝试阅读错误消息? The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
  • 这是一个毫无意义的错误,因为它安装在我的 PC/Linux 容器上,我可以在不使用 Serilog 的情况下毫无问题地执行应用程序(在 Windows 或 Linux 容器上)。你试过读我的问题吗?
  • 请分享Dockerfile
  • @Ziaullah Kha 我已经添加了它。它是标准的 VS 生成的
  • @E.Benedos 你创建了一个工作服务,而不是一个网络应用程序。项目和 docker 文件都不包含 ASP.NET Core。该错误清楚地说明缺少 ASP.NET Core。你想做什么?你真的想创建一个网络应用程序吗?还是你添加了错误的 Serilog 包?

标签: docker serilog .net-core-3.1 microsoft.extensions.hosting


【解决方案1】:

错误很明显 - 缺少 ASP.NET Core 运行时。这甚至不是 Visual Studio 错误,因为 Worker Service 模板旨在创建后台进程,而不是 Web 应用程序。 .NET 运行时本身不包括 ASP.NET Core 运行时。

如果您想在辅助服务中使用 Serilog 和 Microsoft.Extensions.Logging,请使用 Serilog.Extensions.HostingSerilog.Extensions.Logging 包,而不是 Serilog.AspNetcoreAspNetCore 包将特定于 ASP.NET Core 的扩展添加到 Serilog.Extensions.Hosting

如果您确实想将具有长时间运行服务的 Web 应用程序托管为守护程序,则需要将运行时更改为 `aspnet.这也显示在Dockerize an ASP.NET Core application 中。示例 docker 文件使用:

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

不过,更好的解决方案是使用 ASP.NET Core 模板之一并向其添加 BackgroundService。

【讨论】:

  • 现在我明白了。多亏了你的建议和这个(指南)[github.com/serilog/serilog-extensions-hosting],我修复了它。问题在于,在网络上,对于嵌入在 Worker Service .Net Core 应用程序中的外部记录器存在很多混淆。
  • 您需要了解所有 .NET Core 项目仍然是 .NET Core 项目。您不会找到任何将工作人员服务视为特殊类型项目的文章,因为它们不是。它们是带有 BackgroundService 的控制台应用程序,并且只是一个用于在 Windows 或 Linux 上托管的额外包。如果你不需要创建 Windows 服务,你可以只创建一个控制台应用程序并调用 AddHostedService 来获得一个长时间运行的工作人员
  • 我知道 .NetCore 和 .AspNetCore 项目之间的区别(我主要制作 .AspNetCore)。就我而言,我需要一个 WorkerService,因为我会将它用作 linux systemd 服务。网上有很多文章建议您在工作人员服务上安装xxx.AspNetCore(和其他扩展名)以使用外部记录器,这让我很受启发。在 StackOverflow 上还有一个经过验证的(答案)[stackoverflow.com/questions/58292980/…,说明在所需的包上还有Serilog.AspNetCore Version=3.0.0
  • @E.Benedos 这是一个关于 ASP.NET Core 的问题
  • 现在很清楚了,但是(这个问题只是一个例子)它非常受欢迎,因为问题标题和标签引用了 .Net Core。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 2021-06-22
  • 2020-06-29
相关资源
最近更新 更多