Tag Archives: .NET 5

Containerized .NET app

Create a console app

I have created a new .NET 5 console app.

ConsoleApp1.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Program.cs

using System;

Console.WriteLine("Hello World!");

I have published the app in a folder.

In the publish folder I found the below files.

To test the published app, I have opened a terminal and navigate to the published folder and execute below command.

dotnet .\ConsoleApp1.dll

This should print ‘Hello World!’ in the terminal.

Create container image with Docker

I have WSL 2 installed in my Windows 10 machine and I have Ubuntu installed. I have installed Docker desktop for WSL. I have created a new folder ‘ConsoleApp1’ in the Ubuntu home folder and put the published artefacts inside that.

I have opened Widows Terminal and connect Ubuntu Bash with it. Navigate to the ‘ConsoleApp1’ folder. Type code . to open VS Code.

Add a new file with name ‘Dockerfile’ in the folder.

In VS Code write the below code in the Dockerfile.

FROM mcr.microsoft.com/dotnet/runtime:5.0
COPY . ./app
WORKDIR /app
ENTRYPOINT ["dotnet", "ConsoleApp1.dll"]

In the terminal run below command to create a Docker image.

docker build -t 45862391/consoleapp1:v1 .

Check the image successfully created or not.

docker images

To push the image to Docker Hub, run the below command in terminal.

docker push 45862391/consoleapp1:v1

Create a container with Azure Container Instances

Open Azure Portal and create a new Container Instance resource.

After the successful creation of the resource I can check the logs for the output of my console app.

Now my .NET 5 console app is deployed to Azure Container Instance from Docker Hub image.

Summary

  1. Create a .NET 5 console app.
  2. Publish it in a folder.
  3. Create a Dockerfile.
  4. Create Docker image and push to Docker Hub.
  5. Create a Azure container instance based on Docker Hub image.

ASP.NET Core background tasks

To write tasks which runs in background, I always used Windows Task Scheduler or Windows Service. But ASP.NET Core is now more cross platform and these solutions are only for Windows. So recently I found a more cross platform solution which is ASP.NET Core background tasks.

Implement background tasks in microservices with IHostedService and the BackgroundService class | Microsoft Docs

Created some app with this.

GitHub – Arnab-Developer/api-health-check: Api health check app.

GitHub – Arnab-Developer/find-duplicate-bookings: Demo .NET background task application

For hosting I have below options.

  • I can use a VM and deploy my app as a self contained application in a folder. Run the exe file in it.
  • I can use a VM and install .NET 5 runtime just to run a console application. Deploy my application in a folder. I can open a terminal and use dotnet myapp.dll to run it.
  • I can put the app in a container and deploy in azure container instance or app service or kubernetes. I can put the container image in Docker Hub or Azure Container registry.