Local Development with Aspire, Blazor & SQL Server in a container

Aspire is a powerful tool that enhances the developer experience and boosts productivity. Developers generally prefer focusing on writing code rather than managing every aspect of a solution’s infrastructure, especially for components they aren’t actively working on. Setting up things like a local database server or configuring complex environments can be tedious and distracting from the core task of coding.

In this example, I’ll show you how to seamlessly integrate a local SQL Server in a Docker container, allowing you to run your application with minimal setup and zero friction.

To follow this small tutorial, I expect that you already have an Aspire Solution prepared. Including docker.

To add a SQL Server to you Aspire Solution, only the following code is required in Program.cs of the AppHost.

var builder = DistributedApplication.CreateBuilder(args);
var password = builder.AddParameter("SqlServerSaPassword", secret: true);

// The database server including the db called "sqldb"
var sql = builder.AddSqlServer("sql", password);
var sqldb = sql.AddDatabase("sqldb");

// The Blazor App
builder
    .AddProject<Projects.Customer_Web>("customer-web")
    .WithReference(sqldb);

builder. Build().Run();

Because the password is „critical“ it should not appear in the code directly. It is stored in the appsettings.json file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Aspire.Hosting.Dcp": "Warning"
    }
  },
  "Parameters": {
    "SqlServerSaPassword": "topSecret#2024"
  }
}

To connect the Blazor app to the database, simply add the following code in Program.cs.

string connectionString = builder.Configuration.GetConnectionString("sqldb"); 
builder.Services.AddDbContext&lt;AppDbContext>(options => options.UseSqlServer(connectionString));

Just press F5 and you have …


If this is your first time pulling a SQL Server container, you’ll notice the container named ’sql‘ remains in starting up mode in the Resource List for a few moments. You can track the pull progress from the container registry by viewing the logs under Actions. The speed of this process depends on your internet connection.To connect locally to the running SQL container using a tool like Azure Data Studio, you’ll need details like port numbers and container mappings. Simply click on the container (as shown in the image above) to view these details.

Azure Data Studio is a free and platform independent tool for working with common Databases locally and on Azure. You can download it here: Download.

Use this information in the connection dialog of the Azure Data Studio tool.

With that you have a connection to your local SQL Server.

Summary

With this approach you have a local environment with a SQL Server and Database up and running without any problems.

You can find the code here on GitHub: oliverscheer/sample-customer-app: A sample app to demonstrate common features

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen