Skip to content

TryParseOldStyleProjects throws "Sequence contains more than one element" when ASP.NET project contains project references #685

@Sebbs128

Description

@Sebbs128

Describe the bug

AddIISExpressProject from Aspire.Hosting.IncrementalMigration will throw an InvalidOperationException ("Sequence contains more than one element") if the referenced ASP.NET project itself contains project reference (which would be all but the most simple sites) and a launchProfile.json doesn't exist (i.e. reading from the old-style csproj itself).

In IISExpressLaunchDetailsExtensions.TryParseOldStyleProjects, after the csproj is loaded as an xml document, an attempt to grab the root <Project> is made (doc.Descendants(MsbuildNS + "Project").SingleOrDefault()). However Descendants grabs all <Project> nodes, which includes those that are children of <ProjectReference>, which trips the SingleOrDefault.

To Reproduce

Create a class library project and add it as a project reference to the ASP.NET Framework project referenced by the Aspire AppHost.

This repo+branch also reproduced the issue:
https://github.com/Sebbs128/AspireNetFramework/tree/update-to-swa-hosting-package

Exceptions (if any)

System.InvalidOperationException 'Sequence contains more than one element'
Stacktrace

   at System.Linq.ThrowHelper.ThrowMoreThanOneElementException()
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Aspire.Hosting.IISExpressLaunchDetailsExtensions.TryParseOldStyleProject(String path, IISExpressLaunchDetails& metadata)
   at Aspire.Hosting.IISExpressLaunchDetailsExtensions.GetLaunchDetails(IProjectMetadata metadata)
   at Aspire.Hosting.IISExpressProjectExtensions.AddIISExpressProject[TProject](IDistributedApplicationBuilder builder, String name)
   at Program.<Main>$(String[] args) in D:\\source\\AspireNetFramework\\samples\\AspireNetFramework.Samples.AppHost\\Program.cs:line 9

Further technical details

Please include the following if applicable:

ASP.NET Framework Application:

  • Technologies and versions used (i.e. MVC/WebForms/etc): MVC
  • .NET Framework Version: 4.8
  • IIS Version: IISExpress 10.0.26013.1000
  • Windows Version: Windows 11 (100.22621)

ASP.NET Core Application:

  • Targeted .NET version: 10
  • .NET SDK version: 10.0.300

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Triage 🔍Label added to new issues which need Triage

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions