如何在线运行.NET Core 2.0控制台应用程序?
How to Keep Your .NET Core 2.0 Console App Running 24/7 & Choose the Right Hosting
Hey there! Let's break down your problem step by step—getting your console app running nonstop and picking a hosting solution that fits your needs perfectly.
Option 1: Self-Host on a Linux VPS (Full Control)
If you don't mind a bit of hands-on setup, a cheap Linux VPS is a solid choice. It gives you full control over the environment, and you can easily keep your app running with systemd (Linux's built-in process manager). Here's how to do it:
- Pick a VPS: Look for affordable Linux VPS providers that support distros like Ubuntu or CentOS (most budget options do). You don’t need heavy resources for a console app—even the smallest tier will work.
- Deploy your app: Use
scpor an FTP tool to upload your publishedname.dlland all its dependencies to the VPS (make sure you’ve rundotnet publish -c Releaselocally first to get the production-ready files). - Set up a systemd service: This ensures your app starts on boot and restarts automatically if it crashes. Create a service file at
/etc/systemd/system/your-app.servicewith this content:[Unit] Description=My .NET Core Data Sync Console App After=network.target mysql.service # Wait for network and MySQL to be ready [Service] WorkingDirectory=/var/your-app-folder ExecStart=/usr/bin/dotnet /var/your-app-folder/name.dll Restart=always # Restart on crash, unexpected exit, etc. User=non-root-user # Use a non-root user for better security Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target - Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable your-app.service # Configure to start on server boot sudo systemctl start your-app.service - Check status and logs:
sudo systemctl status your-app.service # Verify if the app is running journalctl -u your-app.service -f # View real-time logs for troubleshooting
Option 2: Managed Hosting (Less Maintenance)
If you’d rather skip server admin work, here are the right types of hosting to look for:
- Linux Hosting with SSH Access: Many "ASP.NET hosting" providers offer Linux plans with SSH access. This lets you deploy your console app just like you would on a VPS, and you can still use systemd to manage the process. Avoid providers that only support web apps—they might block non-Kestrel processes.
- Container Hosting: Package your app into a Docker container and deploy it to a container hosting service. These platforms automatically restart your container if it stops, so you get 24/7 uptime without managing the server. A simple Dockerfile for your app would look like:
FROM microsoft/dotnet:2.0-runtime WORKDIR /app COPY ./publish . # Path to your locally published app files ENTRYPOINT ["dotnet", "name.dll"] - Avoid Pure Web-Only Hosting: Some cheap ASP.NET hosts only support web apps running Kestrel. These won’t work for your console app, so always check if they allow custom process execution or SSH access before signing up.
Key Tips for Smooth Operation
- MySQL Connectivity: If your MySQL database is remote, make sure your hosting server’s IP is allowed in the database’s firewall settings. If you host MySQL on the same server, use
localhostin your connection string. - Logging: Set up a logging framework like Serilog or NLog to write logs to a file or remote service. This is critical for troubleshooting if your app stops working unexpectedly.
- .NET Core 2.0 Compatibility: Confirm your hosting environment supports .NET Core 2.0. If not, you can either install the runtime manually (on a VPS) or use a Docker image that includes the 2.0 runtime to guarantee compatibility.
内容的提问来源于stack exchange,提问作者utop




