In many of our articles, we often find ourselves needing to create services to showcase various IT concepts such as TLS, DNS, and Understanding HTTP. This article is all about creating simple dotnet 8 apps that you can use to test out these IT concepts in a practical way. So, let's dive in and create some awesome apps together!
Create App1 and App2 and app3
1. Install the .NET Core SDK
Open a terminal window on your Linux machine and follow the official Microsoft documentation for Linux to install the .NET Core SDK: https://docs.microsoft.com/en-us/dotnet/core/install/linux
2. Once the .NET Core SDK is installed, open the terminal and execute the following command:
cd srv
dotnet new webapi -n app1
This will navigate to the "srv" directory, which is commonly used for web API services, and create a new directory named "app1" with the basic structure of a .NET Core Web API project.
3. Navigate to the project directory and Open the project in a code/text editor
cd app1
vim Program.cs
4. Clear the existing content and replace it with the following code snippet:
5. Modify launchSettings.json
vim Properties/launchSettings.json
Update the content to the following, using port 7001 for the first app (App1):
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "display",
"applicationUrl": "http://localhost:7001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
6. Build and run the application
dotnet build
dotnet run
You will see the following output:
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:7001
...
7. Test the application using curl
The response should be:
Great job! You have successfully created App1. Now, let's move on to working with App2 and App3.
Create App2 and App3
To create App2 and App3 based on App1 and make the necessary modifications to Program.cs and launchSettings.json, follow these steps:
1. Create App2 by copying App1 and making the required changes:
cp -r app1 app2
vim app2/Program.cs
In the app2/Program.cs file, update the number to 2 as needed.
app.MapGet("/", () => "*** App 2 ***\n");
2. Similarly, create App3 by copying App1 and making the necessary changes:
cp -r app1 app3
vim app3/Program.cs
In the app3/Program.cs file, update the number to 3 as needed.
app.MapGet("/", () => "*** App 3 ***\n");
3. Modify the launchSettings.json file for each app to specify the desired ports ( 7002 and 7003 respectively):
vim app2/Properties/launchSettings.json
vim app3/Properties/launchSettings.json
In both files, update the port numbers to 7002 and 7003 respectively.
4. Run both apps
5. Test them using curl:
6. Verify that you see the expected output:
For App2, the expected output is "*** App 2 ***".
For App3, the expected output is "*** App 3 ***".
Great job! you have successfully created three different apps: App1, App2, and App3. These apps can be utilized in various test cases and scenarios.
Dotnet App as a Linux service
I recommend to run ASP.NET Core Web Application as a service on Linux for enhanced manageability and control.
To achieve this, follow these steps:
vim /etc/systemd/system/App1.service
Paste the following content into the file, ensuring that you adjust the paths and port according to your specific application (e.g., App1, App2, App3):
[Unit]
Description=App1 ASP.NET Core Web Application
After=network.target
[Service]
WorkingDirectory=/srv/app1
ExecStart=/usr/bin/dotnet /srv/app1/bin/Debug/net8.0/app1.dll
Restart=always
# Restart service after 10 seconds if it crashes
RestartSec=10
SyslogIdentifier=App1
User=www-data
Environment=ASPNETCORE_URLS=http://localhost:7001
[Install]
WantedBy=multi-user.target
Refresh the systemd configuration, and then enable and start the App1 service:
systemctl daemon-reload
systemctl enable App1.service & systemctl start App1.service & systemctl status App1.service
The service is running
Now let's test it using curl :
Output:
And there you have it!
Your dotnet application is now up and running as a systemd service.
Make sure to repeat the same process for App2 and App3.
I hope you enjoyed this article ♡
Comments