Skip to content

Search for slnx files when setting solution-relative content root #61305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Hosting/TestHost/src/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.ConfigureTestConta
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.ConfigureTestServices(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! webHostBuilder, System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection!>! servicesConfiguration) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.GetTestClient(this Microsoft.AspNetCore.Hosting.IWebHost! host) -> System.Net.Http.HttpClient!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.GetTestServer(this Microsoft.AspNetCore.Hosting.IWebHost! host) -> Microsoft.AspNetCore.TestHost.TestServer!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! applicationBasePath, string! solutionName = "*.sln") -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! solutionName = "*.sln") -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! applicationBasePath, string! solutionName) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! applicationBasePath, System.ReadOnlySpan<string!> solutionNames = default(System.ReadOnlySpan<string!>)) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! solutionName) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
Copy link
Member

@halter73 halter73 Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes should go in the PublicAPI.Unshipped.txt file. For the cases where you removed the default value for the optional solutionName parameter, you can prefix the old API with *REMOVED* and then re-add it without the default value. You can take a look at the OpenApi PublicAPI.Unshipped.txt for reference.

static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseTestServer(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseTestServer(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, System.Action<Microsoft.AspNetCore.TestHost.TestServerOptions!>! configureOptions) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder!
static Microsoft.AspNetCore.TestHost.WebHostBuilderFactory.CreateFromAssemblyEntryPoint(System.Reflection.Assembly! assembly, string![]! args) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder?
Expand Down
56 changes: 48 additions & 8 deletions src/Hosting/TestHost/src/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Microsoft.AspNetCore.TestHost;
/// </summary>
public static class WebHostBuilderExtensions
{
private static readonly string[] _defaultSolutionNames = ["*.sln", "*.slnx"];

/// <summary>
/// Enables the <see cref="TestServer" /> service.
/// </summary>
Expand Down Expand Up @@ -115,20 +117,32 @@ public static IWebHostBuilder ConfigureTestContainer<TContainer>(this IWebHostBu
return webHostBuilder;
}

/// <summary>
/// Sets the content root of relative to the <paramref name="solutionRelativePath" />.
/// </summary>
/// <param name="builder">The <see cref="IWebHostBuilder"/>.</param>
/// <param name="solutionRelativePath">The directory of the solution file.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder UseSolutionRelativeContentRoot(
this IWebHostBuilder builder,
string solutionRelativePath)
{
return builder.UseSolutionRelativeContentRoot(solutionRelativePath, AppContext.BaseDirectory, _defaultSolutionNames);
}

/// <summary>
/// Sets the content root of relative to the <paramref name="solutionRelativePath" />.
/// </summary>
/// <param name="builder">The <see cref="IWebHostBuilder"/>.</param>
/// <param name="solutionRelativePath">The directory of the solution file.</param>
/// <param name="solutionName">The name of the solution file to make the content root relative to.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
[SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Required to maintain compatibility")]
public static IWebHostBuilder UseSolutionRelativeContentRoot(
this IWebHostBuilder builder,
string solutionRelativePath,
string solutionName = "*.sln")
string solutionName)
{
return builder.UseSolutionRelativeContentRoot(solutionRelativePath, AppContext.BaseDirectory, solutionName);
return builder.UseSolutionRelativeContentRoot(solutionRelativePath, AppContext.BaseDirectory, [solutionName]);
}

/// <summary>
Expand All @@ -144,19 +158,45 @@ public static IWebHostBuilder UseSolutionRelativeContentRoot(
this IWebHostBuilder builder,
string solutionRelativePath,
string applicationBasePath,
string solutionName = "*.sln")
string solutionName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think we can remove the [SuppressMessage(... from this overload now too.

It's really unfortunate that we have all these overloads of just string parameters and we decided to add applicationBasePath to the middle of the parameter list instead of the end. That's what RS0026 was warning about. It makes it very difficult to make backwards compatible changes, but I think this change is safe. I'm glad the new overload at least takes a type other than string.

{
return builder.UseSolutionRelativeContentRoot(solutionRelativePath, applicationBasePath, [solutionName]);
}

/// <summary>
/// Sets the content root of relative to the <paramref name="solutionRelativePath" />.
/// </summary>
/// <param name="builder">The <see cref="IWebHostBuilder"/>.</param>
/// <param name="solutionRelativePath">The directory of the solution file.</param>
/// <param name="applicationBasePath">The root of the app's directory.</param>
/// <param name="solutionNames">The names of the solution files to make the content root relative to. If empty, defaults to *.sln and *.slnx.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
[SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Required to maintain compatibility")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Required to maintain compatibility")]

Is this still necessary considering it's now the only overload of UseSolutionRelativeContentRoot that has a default parameter?

public static IWebHostBuilder UseSolutionRelativeContentRoot(
this IWebHostBuilder builder,
string solutionRelativePath,
string applicationBasePath,
ReadOnlySpan<string> solutionNames = default)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use IReadOnlyCollection<string> or similar instead? It's not exactly performance sensitive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ReadOnlySpan<string> is fine, but we can discuss this in the API proposal issue.

{
ArgumentNullException.ThrowIfNull(solutionRelativePath);
ArgumentNullException.ThrowIfNull(applicationBasePath);

if (solutionNames.IsEmpty)
{
solutionNames = _defaultSolutionNames;
}

var directoryInfo = new DirectoryInfo(applicationBasePath);
do
{
var solutionPath = Directory.EnumerateFiles(directoryInfo.FullName, solutionName).FirstOrDefault();
if (solutionPath != null)
foreach (var solutionName in solutionNames)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a LINQ query instead if you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the foreach is fine.

{
builder.UseContentRoot(Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath)));
return builder;
var solutionPath = Directory.EnumerateFiles(directoryInfo.FullName, solutionName).FirstOrDefault();
if (solutionPath != null)
{
builder.UseContentRoot(Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath)));
return builder;
}
}

directoryInfo = directoryInfo.Parent;
Expand Down
196 changes: 196 additions & 0 deletions src/Hosting/TestHost/test/UseSolutionRelativeContentRootTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.TestHost;

public class UseSolutionRelativeContentRootTests : IDisposable
{
private readonly string _tempDirectory;
private readonly string _contentDirectory;

public UseSolutionRelativeContentRootTests()
{
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")[..8]);
_contentDirectory = Path.Combine(_tempDirectory, "src");
Directory.CreateDirectory(_contentDirectory);
}

[Fact]
public void UseSolutionRelativeContentRoot_FindsSlnFile()
{
var solutionFile = Path.Combine(_tempDirectory, "TestApp.sln");
File.WriteAllText(solutionFile, "Microsoft Visual Studio Solution File, Format Version 12.00");

var builder = new WebHostBuilder()
.UseTestServer()
.Configure(app => { });

builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory);

using var host = builder.Build();
var environment = host.Services.GetRequiredService<IWebHostEnvironment>();

Assert.Equal(_contentDirectory, environment.ContentRootPath);
}

[Fact]
public void UseSolutionRelativeContentRoot_FindsSlnxFile()
{
var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx");
File.WriteAllText(solutionFile, """
<Solution>
<Configurations>
<Configuration Name="Debug|Any CPU" />
<Configuration Name="Release|Any CPU" />
</Configurations>
</Solution>
""");

var builder = new WebHostBuilder()
.UseTestServer()
.Configure(app => { });

builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory);

using var host = builder.Build();
var environment = host.Services.GetRequiredService<IWebHostEnvironment>();

Assert.Equal(_contentDirectory, environment.ContentRootPath);
}

[Fact]
public void UseSolutionRelativeContentRoot_WithSolutionName_FindsSpecifiedFile()
{
var subDirectory = Path.Combine(_tempDirectory, "sub");
Directory.CreateDirectory(subDirectory);

var slnFile = Path.Combine(subDirectory, "TestApp.sln");
var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx");
File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00");
File.WriteAllText(slnxFile, """
<Solution>
<Configurations>
<Configuration Name="Debug|Any CPU" />
</Configurations>
</Solution>
""");

var builder = new WebHostBuilder()
.UseTestServer()
.Configure(app => { });

builder.UseSolutionRelativeContentRoot("src", _tempDirectory, "*.slnx");

using var host = builder.Build();
var environment = host.Services.GetRequiredService<IWebHostEnvironment>();

Assert.Equal(_contentDirectory, environment.ContentRootPath);
}

[Fact]
public void UseSolutionRelativeContentRoot_WithMultipleSolutionNames_FindsInCurrentDirectoryFirst()
{
var expectedPath = Path.Combine(_contentDirectory, "sub");
Directory.CreateDirectory(expectedPath);

var slnFile = Path.Combine(_tempDirectory, "TestApp.sln");
var slnxFile = Path.Combine(_contentDirectory, "TestApp.slnx");
File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00");
File.WriteAllText(slnxFile, """
<Solution>
<Configurations>
<Configuration Name="Debug|Any CPU" />
</Configurations>
</Solution>
""");

var builder = new WebHostBuilder()
.UseTestServer()
.Configure(app => { });

builder.UseSolutionRelativeContentRoot("sub", _contentDirectory, ["*.sln", "*.slnx"]);

using var host = builder.Build();
var environment = host.Services.GetRequiredService<IWebHostEnvironment>();

Assert.Equal(expectedPath, environment.ContentRootPath);
}

[Fact]
public void UseSolutionRelativeContentRoot_WithMultipleSolutionNames_WorksWithMultipleFiles()
{
var slnFile = Path.Combine(_tempDirectory, "TestApp.sln");
var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx");
File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00");
File.WriteAllText(slnxFile, """
<Solution>
<Configurations>
<Configuration Name="Debug|Any CPU" />
</Configurations>
</Solution>
""");

var builder = new WebHostBuilder()
.UseTestServer()
.Configure(app => { });

builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory, solutionNames: ["*.sln", "*.slnx"]);

using var host = builder.Build();
var environment = host.Services.GetRequiredService<IWebHostEnvironment>();

Assert.Equal(_contentDirectory, environment.ContentRootPath);
}

[Fact]
public void UseSolutionRelativeContentRoot_ThrowsWhenSolutionNotFound()
{
var builder = new WebHostBuilder()
.UseTestServer()
.Configure(app => { });

var exception = Assert.Throws<InvalidOperationException>(() =>
builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory));

Assert.Contains("Solution root could not be located", exception.Message);
Assert.Contains(_tempDirectory, exception.Message);
}

[Fact]
public void UseSolutionRelativeContentRoot_WithSolutionName_SearchesParentDirectories()
{
var subDirectory = Path.Combine(_tempDirectory, "sub", "folder");
Directory.CreateDirectory(subDirectory);

var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx");
File.WriteAllText(solutionFile, """
<Solution>
<Configurations>
<Configuration Name="Debug|Any CPU" />
</Configurations>
</Solution>
""");

var builder = new WebHostBuilder()
.UseTestServer()
.Configure(app => { });

builder.UseSolutionRelativeContentRoot("src", subDirectory, "*.slnx");

using var host = builder.Build();
var environment = host.Services.GetRequiredService<IWebHostEnvironment>();

Assert.Equal(_contentDirectory, environment.ContentRootPath);
}

public void Dispose()
{
if (Directory.Exists(_tempDirectory))
{
Directory.Delete(_tempDirectory, recursive: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using BasicWebSite;

namespace Microsoft.AspNetCore.Mvc.FunctionalTests;

public class WebApplicationFactorySlnxTests : IClassFixture<WebApplicationFactory<BasicWebSite.Startup>>, IDisposable
{
private readonly string _tempDirectory;
private readonly string _contentDirectory;

public WebApplicationFactorySlnxTests(WebApplicationFactory<BasicWebSite.Startup> factory)
{
Factory = factory;
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")[..8]);
_contentDirectory = Path.Combine(_tempDirectory, "BasicWebSite");

Directory.CreateDirectory(_tempDirectory);
Directory.CreateDirectory(_contentDirectory);

// Create a minimal wwwroot directory to satisfy content root expectations
var wwwrootDir = Path.Combine(_contentDirectory, "wwwroot");
Directory.CreateDirectory(wwwrootDir);
}

public WebApplicationFactory<BasicWebSite.Startup> Factory { get; }

[Fact]
public async Task WebApplicationFactory_UsesSlnxForSolutionRelativeContentRoot()
{
// Create .slnx file in temp directory
var slnxFile = Path.Combine(_tempDirectory, "TestSolution.slnx");
File.WriteAllText(slnxFile, """
<Solution>
<Configurations>
<Configuration Name="Debug|Any CPU" />
<Configuration Name="Release|Any CPU" />
</Configurations>
<Folder Name="/BasicWebSite/">
<Project Path="BasicWebSite/BasicWebSite.csproj" />
</Folder>
</Solution>
""");

var factory = Factory.WithWebHostBuilder(builder =>
{
builder.UseSolutionRelativeContentRoot("BasicWebSite", _tempDirectory, "TestSolution.slnx");
});

using var client = factory.CreateClient();

// Verify that the content root was set correctly by accessing the environment
var environment = factory.Services.GetRequiredService<IWebHostEnvironment>();
Assert.Equal(_contentDirectory, environment.ContentRootPath);
Assert.True(Directory.Exists(environment.ContentRootPath));

// Verify the factory is functional with the .slnx-resolved content root
var response = await client.GetAsync("/");
Assert.True(response.IsSuccessStatusCode);
}

public void Dispose()
{
if (Directory.Exists(_tempDirectory))
{
Directory.Delete(_tempDirectory, recursive: true);
}
}
}
Loading