liveBook · Manning

Connection string

As explained in Building Distributed Applications that Work

A structured configuration value specifying how an application connects to a database.

A connection string gives a client the configuration it needs to reach a resource. In an Aspire application, a resource name links a service’s configuration to a connection string supplied or generated by the AppHost. The service can resolve that name without keeping a separate copy of the value in its own settings. section 3.1 section 5.2.1

The source of the value depends on the resource:

  • For an existing database, register `builder.AddConnectionString("database")` in the AppHost. Put the actual value under `ConnectionStrings:database` in the AppHost’s settings, then pass the resource reference to services that need it. section 3.1
  • For a hosted resource, the connection string can be constructed from its endpoint. A MailDev resource implements `IResourceWithConnectionString` and builds an `smtp://` value from its SMTP host and port. section 2.7.1

Extracting an endpoint for Semantic Kernel

An Ollama model reference named `phi35` places a value under `ConnectionStrings:phi35`. The backend reads it with `builder.Configuration.GetConnectionString("phi35")`, uses `DbConnectionStringBuilder` to find its `Endpoint` value, and passes that URI to `AddOllamaChatCompletion`. If `Endpoint` is missing, it throws an `InvalidDataException` rather than registering a misconfigured client. section 5.2.1

DbConnectionStringBuilder csBuilder = new()
{
    ConnectionString = phiConnectionString
};

if (!csBuilder.TryGetValue("Endpoint", out var ollamaEndpoint))
{
    throw new InvalidDataException(
        "Ollama connection string is not properly configured.");
}

The parser checks for the endpoint before the backend uses it. section 5.2.1

How Building Distributed Applications that Work covers it

How deep it goes
Shows registration, settings lookup, endpoint extraction, and resource-generated values; defers internally hosted database setup.
Examples
Uses an existing database, an Ollama model consumed by Semantic Kernel, and a MailDev SMTP resource.
What it assumes
Assumes familiarity with .NET application configuration and service registration while showing the Aspire connection-string steps.
What you can do after
Apply it via Aspire to supply an existing database connection or extract and validate an Ollama endpoint.
How it connects
Links connection strings to Aspire service discovery, application settings, resource references, and endpoint configuration.

Also explained in

Compare how 7 books explain connection string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.