PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 2, 2022

[FIXED] How to run a ASP.NET Core Web API dll file as a service in Nginx server?

 September 02, 2022     .net, asp.net-core, asp.net-core-webapi, nginx-reverse-proxy, ubuntu     No comments   

Issue

I have an AWS EC2 instance running on Ubuntu 16.04 server. I am running an ASP.NET Core Web API server in this instance.

I have followed this link to host ASP.NET Core on Linux with Nginx. I have followed the tutorial until the Monitoring the app section. Now, i have run the web api dll file using below command and it is listening at http://localhost:5000

dotnet MyWebAPI.dll

I have run this above command by connecting with the EC2 instance using PuTTY. As i have set up the reverse proxy server, so i can hit my endpoint nicely using postman.

But, when i close the PuTTY session, the dll file is not running anymore. As a result, i could not hit the endpoint.

So, what should i do to keep the dll file running on localhost as a service so that it does not stop when i close the PuTTY session?


Solution

I could run my application as a service inside the Nginx server. Now, if i close the PuTTY session, the service is still running. I have used systemd to create a service file to start and monitor the underlying web app.

systemd is an init system that provides many powerful features for starting, stopping, and managing processes.

At first, i have created a service file for my web application

sudo nano /etc/systemd/system/my-web-api.service

Then, inside that my-web-api.service file, i have included below configurations:

[Unit] 
Description=My first .NET Core application on Ubuntu 

[Service] 
WorkingDirectory=/home/ubuntu/MyWebAPI 
ExecStart=/usr/bin/dotnet /home/ubuntu/MyWebAPI/MyWebAPI.dll 
Restart=always 
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes 
SyslogIdentifier=offershare-web-app
Environment=ASPNETCORE_ENVIRONMENT=Production 

[Install] 
WantedBy=multi-user.target

Then, i need to enable the service and run it.

sudo systemctl enable my-web-api.service
sudo systemctl start my-web-api.service
sudo systemctl status my-web-api.service

Now, the status command should show the service as running if the configuration is correct. Now, my web application is running, kestrel by default listens on port 5000, so my application is available on http://localhost:5000.

Now, if i close the PuTTY session, my web api is still running.



Answered By - Setu Kumar Basak
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing