Skip to main content

Deploy .NET Apps

.NET installation is powered by dotnet-install script. To install latest version with deployment script:

features:
- dotnet

Without this, dotnet won't be available since no globally installed .NET runtime available.

Install other version using dotnet latest, dotnet 0.9.0, etc. To uninstall use dotnet off. Check the current version using dotnet --version from SSH.

Example

The deployment script below installs the latest dotnet compiler, writes Program.cs and run dotnet run serving HTTP from PORT envar.

https://github.com/domcloud/recipes/blob/master/dotnet.yml
source: clear
features:
- dotnet latest
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT dotnet run
commands:
- dotnet new console --name App
- filename: Program.cs
content: |
using System.Text;
using System.Net;
class HttpServer
{
public static HttpListener listener = new();
public static string pageBody = @"
<!DOCTYPE html>
<html>
<head>
<title>DotNet App</title>
<link rel=""stylesheet"" href=""//unpkg.com/bootstrap/dist/css/bootstrap.min.css"">
</head>
<body class=""p-5 text-center"">
<p><img src=""//images.unsplash.com/photo-1465153690352-10c1b29577f8?fit=crop&w=200&h=200""
class=""img-fluid rounded-circle""></p>
<h1 class=""mb-3"">Hello, world!</h1>
<p>Serving from DotNet version {0}</p>
<p class=""text-muted"">DOM Cloud</p>
</body>
</html>
";


public static async Task HandleIncomingConnections()
{
string version = Environment.Version.ToString();
byte[] pageData = Encoding.UTF8.GetBytes(string.Format(pageBody, version));
while (true)
{
// Will wait here until we hear from a connection
HttpListenerContext ctx = await listener.GetContextAsync();

// Peel out the requests and response objects
HttpListenerResponse resp = ctx.Response;

resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = pageData.LongLength;

await resp.OutputStream.WriteAsync(pageData);
resp.Close();
}
}

public static void Main(string[] args)
{
// Create a Http server and start listening for incoming connections
string? port = Environment.GetEnvironmentVariable("PORT") ?? "3000";
listener.Prefixes.Add($"http://localhost:{port}/");
listener.Start();
Console.WriteLine("Listening for connections on localhost:{0}", port);

// Handle requests
Task listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();

// Close the listener
listener.Close();
}
}

Existing .NET projects

Existing Dotnet projects can run directly from source code. This eliminates the need to compile the code into a binary, although startup time may be a little bit slower to start.

https://github.com/domcloud/recipes/blob/master/dotnet-gls.yml
features:
- dotnet lts
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT dotnet run

App Management

Your app do not restarted automatically after file changes. To restart, run restart via SSH.

Environment variables can be set either using NGINX's env_var_list or ~/.bashrc. Usually your language framework also reads .env files.

See NGINX and App Daemon for more information about NGINX and App managements including restarting, environment variables, and other global limitations.

App Logging

You can see app log from Check -> Check Process Logs tab. Only startup problems displayed in the browser.

You might want to use a proper logging mechanism such as the standard logging library then write it to a log file, or any other solution that suits you.

NGINX errors and traffic logs can be examined via Webmin or Check -> Check Process Logs tab.