Web application in C# Microsoft (.NET) with connection to SQLite

To deploy an application in Amverum, you need to follow these simple steps:

  1. Open the page https://cloud.amverum.com/projects

  2. Click the Create button and select service type application.

  3. We upload all the files (you can use git, or you can use the interface). Don’t forget to add the configuration file (amverum.yml) and/or Dockerfile.

  4. After this, the build and deployment application will begin. Wait for the “Successfully Deployed” status to appear.

Let’s take a closer look at the process.

Let’s write a simple web application that, when opening a website, displays all the information from the database. We will use the ASP.NET web framework and the SQLite database.

Creating a Project

First, let’s create a new project (web application) named «WebService»:

$ dotnet new web -n WebService

The WebService directory has been created, let’s go to it:

$ cd WebService

Since we will use SQLite, we need to add it to the project:

$ dotnet add package System.Data.SQLite

Database (SQLite)

The Program.cs file (code available at the end of the documentation) implements interaction with the database. A separate application/container is not required to use SQLite in Amverum. It is enough to specify in the code the path to store the files of this database in permanent storage. You can read more details here.

Important: Pay attention to the specified database path. Changeable files (databases, etc.) need to be saved in permanent storage /data. This will avoid their loss during reassembly. The data folder in the code and the permanent storage /data are different directories.

Configuration

amverum.yml

You can write a yaml file yourself or fill it out in the “Configuration” section of your personal account.

Sample amverum.yml file:

meta:
  environment: csharp
  toolchain:
    name: dotnet
    version: 8.0
build:
  image: mcr.microsoft.com/dotnet/sdk:8.0
run:
  image: mcr.microsoft.com/dotnet/sdk:8.0
  buildFileName: bin/WebService
  persistenceMount: /data
  containerPort: 8080

Important: The buildFileName field must be in the format bin/file_name, or bin/publish/file_name if you are doing it via publishing.

Important: Pay attention to containerPort. When creating an application in Program.cs the port is not explicitly specified, so the default value (8080) is used. If an incorrect port is specified in the amverum.yml file, a «Service Unavailable 503» error will occur. Amverum listens on port 80 by default.

Let’s consider an alternative way to set the configuration - via Dockerfile.

Dockerfile

Note: if you are using a Dockerfile, then in most cases you do not need to add the amverum.yml configuration file.

Steps:

  1. Create a Dockerfile in the project directory.

  2. In the Dockerfile we specify the base image for the build stage:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

instead of 8.0, you can specify any other version that you need.

  1. Set the working directory inside the container:

WORKDIR /app
  1. Copy the WebService.csproj file to the container:

COPY WebService.csproj ./
  1. Recovering dependencies:

RUN dotnet restore
  1. Copy all project files into the container:

COPY . ./
  1. We publish the application for release:

RUN dotnet publish -c Release -o WebService
  1. Specify the base image for the launch stage:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
  1. Set the working directory inside the container:

WORKDIR /app 
  1. We copy the published result from the build stage:

COPY --from=build /app/WebService ./ 
  1. Set the entry point for the container:

ENTRYPOINT ["dotnet", "WebService.dll"]

The resulting Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
COPY WebService.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o WebService

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/WebService ./
ENTRYPOINT ["dotnet", "WebService.dll"]

Example amverum.yml file when used with Dockerfile:

meta:
  environment: docker
  toolchain:
    name: docker
    version: latest
build:
  dockerfile: Dockerfile
  skip: false
run:
  persistenceMount: /data
  containerPort: 8080

Creating a project in Amverum

The last step is to deploy the application. The Program.cs file contains the main code and connects to the database:

Program.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Data.SQLite;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Specify the path to the database:
var connectionString = "Data Source=../data/people.db;";

using var connection = new SQLiteConnection(connectionString);
connection.Open();
var createTableCommand = connection.CreateCommand();
createTableCommand.CommandText = @"
    CREATE TABLE IF NOT EXISTS People (
        Id INTEGER PRIMARY KEY,
        Name TEXT,
        Age INTEGER,
        Occupation TEXT
    );
";
createTableCommand.ExecuteNonQuery();

var insertDataCommand = connection.CreateCommand();
insertDataCommand.CommandText = @"
    INSERT OR IGNORE INTO People (Name, Age, Occupation) VALUES
    ('John Doe', 30, 'Engineer'),
    ('Jane Smith', 25, 'Teacher'),
    ('Michael Johnson', 35, 'Doctor');
";
insertDataCommand.ExecuteNonQuery();

app.MapGet("/", async () =>
{
    using var selectCommand = connection.CreateCommand();
    selectCommand.CommandText = "SELECT * FROM People;";
    using var reader = await selectCommand.ExecuteReaderAsync();
    
    string result = "People Database:\n";
    while (reader.Read())
    {
        result += $"ID: {reader.GetInt32(0)}, Name: {reader.GetString(1)}, Age: {reader.GetInt32(2)}, Occupation: {reader.GetString(3)}\n";
    }
        
    return result;
});

app.Run();

If necessary, you can change the path to the database.

If you do not want to use the standard port (8080), you can specify another one. But then it is important to take this change into account in the configuration file.

Functionality check

  1. Go to the project settings and activate the domain name.

  2. Now you can click on it and our website will open with records from the database.

If something does not work, we recommend that you read the Build and Application logs.

Important

Save database files and other changeable data to permanent storage to avoid losing them when updating the project when the code folder is “rolled back” to the state of the repository update. The data folder in the project root and the /data directory are different directories.

You can check that the save is going to /data by going to the “data” folder on the “Repository” page.

Important

To avoid the 502 error, change host 127.0.0.1 (or similar localhost) to 0.0.0.0 in your code, and specify in the configuration the port that your application listens to (example - 8080).

Congratulations, you have successfully created your first application in Amverum!

If you are unable to deploy the project

Write the symptoms you observe to support@amverum.ru indicating your username and project name, we will try to help you.