dotnet / dotnet/systemweb-adapters
TryParseOldStyleProjects throws "Sequence contains more than one element" when ASP.NET project contains project references
- Dominant language
- C#
- Stars
- 384
- Forks
- 76
- Avg merge
- 2h 24m
- Merged PRs (30d)
- 1
Description
### 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 `` is made (`doc.Descendants(MsbuildNS + "Project").SingleOrDefault()`). However `Descendants` grabs all `` nodes, which includes those that are children of ``, 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
```plaintext
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.$(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
Contributor guide
Assessment
This issue has not been assessed yet.