Skip to content
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
17 changes: 17 additions & 0 deletions src/Hosting/TestHost/src/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ public static IWebHostBuilder UseSolutionRelativeContentRoot(
}
while (directoryInfo is not null);

if (string.Equals(solutionName, "*.sln", StringComparison.OrdinalIgnoreCase))
{
directoryInfo = new DirectoryInfo(applicationBasePath);
do
{
var solutionPath = Directory.EnumerateFiles(directoryInfo.FullName, "*.slnx").FirstOrDefault();
if (solutionPath != null)
{
builder.UseContentRoot(Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath)));
return builder;
}

directoryInfo = directoryInfo.Parent;
}
while (directoryInfo is not null);
}

throw new InvalidOperationException($"Solution root could not be located using application root {applicationBasePath}.");
}

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

using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Xunit;

namespace Microsoft.AspNetCore.TestHost;

public class WebHostBuilderExtensionsTests
{
[Fact]
public void UseSolutionRelativeContentRoot_FallsBackToSlnx()
{
var rootPath = Path.Combine(Path.GetTempPath(), "slnx-" + Guid.NewGuid().ToString("n"));
var applicationBasePath = Path.Combine(rootPath, "src", "MyApp", "bin");

try
{
Directory.CreateDirectory(applicationBasePath);
File.WriteAllText(Path.Combine(rootPath, "Test.slnx"), string.Empty);
Directory.CreateDirectory(Path.Combine(rootPath, "src", "MyApp"));

var builder = new WebHostBuilder();

builder.UseSolutionRelativeContentRoot("src/MyApp", applicationBasePath, "*.sln");

Assert.Equal(Path.GetFullPath(Path.Combine(rootPath, "src", "MyApp")), builder.GetSetting(WebHostDefaults.ContentRootKey));
}
finally
{
if (Directory.Exists(rootPath))
{
Directory.Delete(rootPath, recursive: true);
}
}
}

[Fact]
public void UseSolutionRelativeContentRoot_PrefersSlnOverSlnx()
{
var rootPath = Path.Combine(Path.GetTempPath(), "sln-" + Guid.NewGuid().ToString("n"));
var applicationBasePath = Path.Combine(rootPath, "child", "content", "bin");

try
{
Directory.CreateDirectory(applicationBasePath);
File.WriteAllText(Path.Combine(rootPath, "Parent.sln"), string.Empty);
File.WriteAllText(Path.Combine(rootPath, "child", "Child.slnx"), string.Empty);
Directory.CreateDirectory(Path.Combine(rootPath, "parentContent"));

var builder = new WebHostBuilder();

builder.UseSolutionRelativeContentRoot("parentContent", applicationBasePath, "*.sln");

Assert.Equal(Path.GetFullPath(Path.Combine(rootPath, "parentContent")), builder.GetSetting(WebHostDefaults.ContentRootKey));
}
finally
{
if (Directory.Exists(rootPath))
{
Directory.Delete(rootPath, recursive: true);
}
}
}
}
Loading