.NET Aspire persistent volume mount sample
This sample demonstrates how to configure a SQL Server container to use a persistent volume mount in .NET Aspire, so that the data is persisted across app launches. This method can be used to persist data across instances of other container types configured in .NET Aspire apps too, e.g. PostgreSQL, Redis, etc.
The app consists of a single service, VolumeMount.BlazorWeb, that is configured with a SQL Server container instance via the AppHost project. PostgreSQL and Azure Storage data services are also configured in the AppHost and Blazor projects for demonstration and experimentation purposes. This Blazor Web app has been setup to use ASP.NET Core Identity for local user account registration and authentication, including the new Blazor identity UI. Using a persistent volume mount means that user accounts created when running locally are persisted across launches of the app.
The app also includes a standard class library project, VolumeMount.ServiceDefaults, that contains the service defaults used by the service project.
Pre-requisites
Running the app
If using Visual Studio, open the solution file
VolumeMount.sln
and launch/debug theVolumeMount.AppHost
project.If using the .NET CLI, run
dotnet run
from theVolumeMount.AppHost
directory.The first time you run the app, you'll receieve an error indicating that a password for the SQL Server container has not been configured. To configure a password, set the
sqlpassword
key in the User Secrets store of theVolumeMount.AppHost
project using thedotnet user-secrets
command in the AppHost project directory:dotnet user-secrets set sqlpassword <password>
A stable password is required when using a persistent volume mount for SQL Server data, rather than the default auto-generated password.
Once a password is configured, run the
VolumeMount.AppHost
project again and navigate to the URL for theVolumeMount.BlazorWeb
from the dashboard.From the home page, click the "Register" link and enter a email and password to create a local user:
After a short while (30-60s) an error page will be displayed stating that the database is not initialized and suggesting that EF Core migration be run. Click the "Apply Migrations" button and once the button text changes to "Migrations Applied", refresh the browser and confirm the form resubmission when prompted by the browser:
A page will be shown confirming the registration of the account and a message detailing that a real email sender is not registered. Find and click the link at the end of the message to confirm the created account:
Verify that the email confirmation page is displayed, indicating that the account is now registered and can be used to login to the site, and then click on the "Login" link in the left-hand navigation menu:
Enter the email and password you used in the account registration page to login:
Once logged in, click the "Logout" link in the left-hand navigation menu to log out of the site, and then stop the app, followed by starting it again, and verifying that the account you just created can still be used to login to the site once restarted, indicating that the database was using the persistent volume to store the data. You can verify the named volume existance using the Docker CLI too (
docker volume ls
):> docker volume ls -f name=sqlserver DRIVER VOLUME NAME local VolumeMount.sqlserver.data