Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/Aspire.Hosting.Testing/Aspire.Hosting.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(SharedDir)LaunchProfiles\LaunchSettingsReader.cs" Link="LaunchSettingsReader.cs" />
<Compile Include="$(SharedDir)LaunchProfiles\LaunchSettingsSerializerContext.cs" Link="LaunchSettingsSerializerContext.cs" />
<Compile Include="$(SharedDir)\KnownConfigNames.cs" Link="KnownConfigNames.cs" />
</ItemGroup>
Expand Down
26 changes: 1 addition & 25 deletions src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -342,30 +341,7 @@ void SetDefault(string key, string? value)
return null;
}

var projectFileInfo = new DirectoryInfo(appHostPath);
var launchSettingsFilePath = projectFileInfo.FullName switch
{
null => Path.Combine("Properties", "launchSettings.json"),
_ => Path.Combine(projectFileInfo.FullName, "Properties", "launchSettings.json")
};

// It isn't mandatory that the launchSettings.json file exists!
if (!File.Exists(launchSettingsFilePath))
{
return null;
}

using var stream = File.OpenRead(launchSettingsFilePath);
try
{
var settings = JsonSerializer.Deserialize(stream, LaunchSettingsSerializerContext.Default.LaunchSettings);
return settings;
}
catch (JsonException ex)
{
var message = $"Failed to get effective launch profile for project '{appHostPath}'. There is malformed JSON in the project's launch settings file at '{launchSettingsFilePath}'.";
throw new DistributedApplicationException(message, ex);
}
return LaunchSettingsReader.GetLaunchSettingsFromDirectory(appHostPath, $"project '{appHostPath}'");
}

private void OnBuilderCreatedCore(DistributedApplicationBuilder applicationBuilder)
Expand Down
15 changes: 1 addition & 14 deletions src/Shared/LaunchProfiles/LaunchProfileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Json;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Resources;

Expand Down Expand Up @@ -109,19 +108,7 @@ internal static class LaunchProfileExtensions
}
}

using var stream = File.OpenRead(launchSettingsFilePath);

try
{
var settings = JsonSerializer.Deserialize(stream, LaunchSettingsSerializerContext.Default.LaunchSettings);
return settings;
}
catch (JsonException ex)
{
var message = $"Failed to get effective launch profile for project resource '{resourceName}'. There is malformed JSON in the project's launch settings file at '{launchSettingsFilePath}'.";
throw new DistributedApplicationException(message, ex);
}

return LaunchSettingsReader.ReadLaunchSettingsFile(launchSettingsFilePath, $"project resource '{resourceName}'");
}

private static readonly LaunchProfileSelector[] s_launchProfileSelectors =
Expand Down
51 changes: 51 additions & 0 deletions src/Shared/LaunchProfiles/LaunchSettingsReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;

namespace Aspire.Hosting;

internal static class LaunchSettingsReader
{
/// <summary>
/// Reads launch settings from the <c>Properties/launchSettings.json</c> file within the specified directory.
/// </summary>
/// <param name="directoryPath">The directory to look for launch settings in. If <see langword="null"/>, a relative path is used.</param>
/// <param name="resourceIdentifier">A descriptive identifier used in error messages when JSON parsing fails.</param>
/// <returns>The deserialized <see cref="LaunchSettings"/>, or <see langword="null"/> if the file does not exist.</returns>
internal static LaunchSettings? GetLaunchSettingsFromDirectory(string? directoryPath, string resourceIdentifier)
{
var launchSettingsFilePath = directoryPath is null
? Path.Combine("Properties", "launchSettings.json")
: Path.Combine(Path.GetFullPath(directoryPath), "Properties", "launchSettings.json");

if (!File.Exists(launchSettingsFilePath))
{
return null;
}

return ReadLaunchSettingsFile(launchSettingsFilePath, resourceIdentifier);
}

/// <summary>
/// Reads and deserializes launch settings from the specified file.
/// </summary>
/// <param name="launchSettingsFilePath">The path to the launch settings file.</param>
/// <param name="resourceIdentifier">A descriptive identifier used in error messages when JSON parsing fails.</param>
/// <returns>The deserialized <see cref="LaunchSettings"/>.</returns>
/// <exception cref="DistributedApplicationException">Thrown when the file is empty or contains malformed JSON.</exception>
internal static LaunchSettings? ReadLaunchSettingsFile(string launchSettingsFilePath, string resourceIdentifier)
{
using var stream = File.OpenRead(launchSettingsFilePath);

try
{
return JsonSerializer.Deserialize(stream, LaunchSettingsSerializerContext.Default.LaunchSettings);
}
catch (JsonException ex)
{
var message = $"Failed to get effective launch profile for {resourceIdentifier}. There is malformed JSON in the project's launch settings file at '{launchSettingsFilePath}'.";
throw new DistributedApplicationException(message, ex);
}
}
}
Loading