Skip to content

🐞 Bug Report: CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects fails with .NET 8 #913

@mmassari

Description

@mmassari

Description

The package CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects is documented to support .NET 8 and .NET 9, but in practice it only works with .NET 9.
When used in a project targeting .NET 8 (LTS), the Aspire host fails to start with a TypeInitializationException caused by missing MSBuild instance detection.

This issue occurs with all package versions from 9.0.0 to 9.8.0.
Upgrading the entire Aspire solution and all related projects to .NET 9 makes it work, but this is not feasible in enterprise environments where only LTS versions (like .NET 8) are allowed.


💥 Actual Behavior

Application startup fails with the following error:

System.TypeInitializationException: 'The type initializer for 'Aspire.Hosting.SqlProjectBuilderExtensions' threw an exception.'

Inner exception:
System.InvalidOperationException: 'No instances of MSBuild could be detected. 
Try calling RegisterInstance or RegisterMSBuildPath to manually register one.'

⚙️ Environment

Component | Version -- | -- Aspire SDK | 9.5.1 CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects | 9.0.0 – 9.8.0 (all affected) .NET SDK | 8.0.x (LTS) OS | Windows 11 x64 IDE | Visual Studio 2022 / Rider

💡 Workaround

Migrating the entire solution (Aspire host and projects) to .NET 9 resolves the issue.
However, this is not viable for enterprise environments restricted to LTS (.NET 8) releases.

Regression

No response

🔄 Reproduction Steps

1. Create a new Aspire project targeting .NET 8 (e.g., Aspire version 9.5.1).

2. Add the following package:

<PackageReference Include="CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects" Version="9.8.0" />


3. In Program.cs, add the following code:

var builder = DistributedApplication.CreateBuilder(args);

var sql = builder.AddSqlServer("sql")
    .WithLifetime(ContainerLifetime.Persistent)
    .WithDataVolume();

var sqlDb = sql.AddDatabase("db");
builder.AddSqlProject<Projects.sql_db_prj>("sql-db-project")
       .WithReference(sqlDb);

4. Run the Aspire host.

✅ Expected Behavior

The SQL project should load and register correctly in Aspire when running under .NET 8, without requiring migration to .NET 9.

Screenshots

No response

IDE and version

VS 2022

IDE version

17.14.18

Nuget packages

CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects 9.8.0

🧩 Additional Context

It seems the MSBuildLocator used by the SqlProjectBuilderExtensions class is unable to detect MSBuild instances when running under .NET 8.
This might be related to the internal version checks or SDK references baked into the package build.

Help us help you

Yes, I'd like to be assigned to work on this item

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions