1

I'm writing a .Net Core 3.1 Worker Service application using Visual Studio 2019 (Docker support enabled).

If I start the app using Docker all works well but when I add Serilog.AspNetCore 3.4.0 dependency to the project I'm not able to debug with docker anymore.

Cases:

  • Start app locally without Serilog --> OK
  • Start app on Docker without Serilog --> OK
  • Start app locally with Serilog --> OK
  • Start app on Docker with Serilog --> ERROR

The Visual Studio returned error is:

enter image description here

and this is the console returned error:

-------------------------------------------------------------------
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).

EDIT:

This is VS 2019 automatically generated 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"]

What is the problem and how I can solve it?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
E.Benedos
  • 1,585
  • 14
  • 41
  • Did you try reading the error message? `The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.` – Ian Kemp Nov 20 '20 at 15:35
  • 1
    It is a no sense error due to the fact that it is installed on my PC/Linux container and I can execute the app with no problem (on Windows or Linux container) without using Serilog. Have you tried to read my question? – E.Benedos Nov 20 '20 at 15:40
  • please share the `Dockerfile` – Ziaullah Khan Nov 22 '20 at 06:48
  • @Ziaullah Kha I've added it. It is the standard VS generated one – E.Benedos Nov 23 '20 at 07:02
  • @E.Benedos you created a worker service, not a web app. Neither the project nor the docker file contain ASP.NET Core. The error clearly explains that ASP.NET Core is missing. What are you trying to do? Are you really trying to create a web app? Or did you add the wrong Serilog package? – Panagiotis Kanavos Nov 23 '20 at 11:13

1 Answers1

3

The error is clear - the ASP.NET Core runtime is missing. This isn't even a Visual Studio bug, as the Worker Service template is meant to create a background process, not a web app. The .NET Runtime alone doesn't include the ASP.NET Core runtime.

If you want to use Serilog with Microsoft.Extensions.Logging in a worker service, use Serilog.Extensions.Hosting or Serilog.Extensions.Logging package, not Serilog.AspNetcore. The AspNetCore package adds ASP.NET Core-specific extensions to Serilog.Extensions.Hosting.

If you really want to host a web app with a long running service as a daemon, you need to change the runtime to `aspnet. This is shown in Dockerize an ASP.NET Core application as well. The example docker file uses :

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

A better solution though, would be to use one of the ASP.NET Core templates and add a BackgroundService to it.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Now I understand. I fix it thanks to your advice and thanks to this (guide) [https://github.com/serilog/serilog-extensions-hosting]. The problem is that on the web there is a lot of confusion about external loggers embed on Worker Service .Net Core apps. – E.Benedos Nov 24 '20 at 10:11
  • 1
    You need to understand that all .NET Core projects are still .NET Core projects. You won't find any articles treating worker services as a special type of project because they aren't. They're console applications with a BackgroundService and just one extra package for hosting on Windows or Linux. If you don't need to create a Windows service, you could just create a console application and call `AddHostedService` to get a long-running worker – Panagiotis Kanavos Nov 24 '20 at 10:16
  • I know the difference between .NetCore and .AspNetCore projects (I made principally .AspNetCore). In my case I need a WorkerService because I will use it as linux systemd service. I was svied by a lot of articles online that advice you to install `xxx.AspNetCore` (and others extension) on worker service in order to use an external logger. Also on StackOverflow there is a verified (answer) [https://stackoverflow.com/questions/58292980/how-to-setup-serilog-seq-for-net-core-3-0-worker-service] that explain that on the needed packages there is also `Serilog.AspNetCore Version=3.0.0` – E.Benedos Nov 24 '20 at 10:29
  • @E.Benedos that was a question about ASP.NET Core – Panagiotis Kanavos Nov 24 '20 at 10:33
  • it is clear now, but (and that question is only an example) it is very svied because question title and tags referers to .Net Core. – E.Benedos Nov 24 '20 at 10:43