Aspire Part 2. How to configure your app to use the SQL container.

Aspire Part 2. How to configure your app to use the SQL container.

This entry is part 2 of 2 in the series .NET Aspire

In Part 1, we talked about setting up a MSSQL docker container for usage in Aspire. As specified before, we setup our MSSQL container as following:

    var sqlServer = builder.AddSqlServer("sqlserver")
                      .WithDataVolume()
                      .WithBindMount("c:\export\db.bacpac", "/var/opt/mssql/backup/db.bacpac") // Mount .bacpac
                      .WithBindMount("Resources/sqlpackage-linux", "/sqlpackage") // Mount sqlpackage
                      .WithBindMount("Resources/import-bacpac.sh", "/sqlpackage/import-bacpac.sh", isReadOnly: false)
                      .WithEntrypoint("/sqlpackage/import-bacpac.sh")
                      .WithEnvironment("ACCEPT_EULA", "Y")
                      .WithEndpoint(1433, 1433, name: "sqlserver-endpoint")
                      .WithLifetime(ContainerLifetime.Persistent);

    var db = sqlServer.AddDatabase("our-database");

   var api = builder.AddProject<Projects.Web_Api>("web-api")
           .WithHttpsEndpoint(port: 44393, targetPort: 5001)
           .WithReference(db)
           .WaitFor(db);

The code is self explanatory, the SQL Server starts, when this is started, the API starts. It’s important to include the WithReference and WaitFor. This tells Aspire to start these components in the right order.

Aspire injects the ‘our-database’ in the configuration which we can use in other apps added to Aspire to retrieve this during app start up without having to hardcode anything. Here’s an example which shows how to a ASP.NET API can determine which SQL connection string to use. When the API starts outside of Aspire, the connection string is retrieved from another config such as keyvault.

            string sqlConnectionString;
            if (Configuration.GetConnectionString("our-database") != null)
            {
                // Aspire environment: Use injected connection string
                sqlConnectionString= Configuration.GetConnectionString("our-database") + ";TrustServerCertificate=True";
            }
            else
            {
                sqlConnectionString= Configuration["SqlConnectionString"];
            }

So, now we have a working API with our local containerized MSSQL. What about apps that depend on the previously mentioned API. That works almost the same way. First, add let’s say a Blazor Server app to Aspire. The same thing as the MSSQL and API.

    builder.AddProject<Projects.Web_Management>("web-management")
        .WithHttpsEndpoint(port: 44373, targetPort: 5101)
        .WithReference(api)
        .WaitFor(api);

In the startup of the Blazor Server app, we need to add the following:

        if (Configuration["services:openline-automation-web-api:https:0"] != null)
        {
            httpclient.BaseAddress = Configuration["services:openline-automation-web-api:https:0"];
        }
        else
        {
            httpclient.BaseAddress = Configuration["ApiUri"];
        }

And again, the same thing as before. When running in Aspire, the Aspire API URI is used. When running outside of Aspire, the API base address is retrieved from config.

When starting Aspire, you’ll notice that all apps started in the correct order.

Series Navigation<< Aspire Part 1. How to setup Aspire to use a local SQL Server container

Leave a Reply

Your email address will not be published. Required fields are marked *