Skip to content

Commit 098c33b

Browse files
authored
PR follow up changes (#1090)
- Remove AssemblyName and AssemblyPath from IServiceMetadata - Make the app host directory available to the app via config
1 parent ff5562c commit 098c33b

8 files changed

+23
-25
lines changed

src/Aspire.Hosting/DistributedApplicationBuilder.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class DistributedApplicationBuilder : IDistributedApplicationBuilder
2929
public IServiceCollection Services => _innerBuilder.Services;
3030

3131
/// <inheritdoc />
32-
public string ProjectDirectory { get; }
32+
public string AppHostDirectory { get; }
3333

3434
/// <inheritdoc />
3535
public IResourceCollection Resources { get; } = new ResourceCollection();
@@ -43,7 +43,13 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options)
4343
_args = options.Args ?? [];
4444
_innerBuilder = new HostApplicationBuilder();
4545

46-
ProjectDirectory = options.ProjectDirectory ?? _innerBuilder.Environment.ContentRootPath;
46+
AppHostDirectory = options.ProjectDirectory ?? _innerBuilder.Environment.ContentRootPath;
47+
48+
// Make the app host directory available to the application via configuration
49+
_innerBuilder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
50+
{
51+
["AppHost:Directory"] = AppHostDirectory
52+
});
4753

4854
// Core things
4955
_innerBuilder.Services.AddSingleton(sp => new DistributedApplicationModel(Resources));

src/Aspire.Hosting/Extensions/ExecutableResourceBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static class ExecutableResourceBuilderExtensions
2323
/// <returns>The <see cref="IResourceBuilder{ExecutableResource}"/>.</returns>
2424
public static IResourceBuilder<ExecutableResource> AddExecutable(this IDistributedApplicationBuilder builder, string name, string command, string workingDirectory, params string[]? args)
2525
{
26-
workingDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.ProjectDirectory, workingDirectory));
26+
workingDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, workingDirectory));
2727

2828
var executable = new ExecutableResource(name, command, workingDirectory, args);
2929
return builder.AddResource(executable);

src/Aspire.Hosting/Extensions/ProjectResourceBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static IResourceBuilder<ProjectResource> AddProject(this IDistributedAppl
4141
{
4242
var project = new ProjectResource(name);
4343

44-
projectPath = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.ProjectDirectory, projectPath));
44+
projectPath = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, projectPath));
4545

4646
return builder.AddResource(project)
4747
.WithProjectDefaults()

src/Aspire.Hosting/IDistributedApplicationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IDistributedApplicationBuilder
1919
/// <summary>
2020
/// Directory of the project where the app host is located. Defaults to the content root if there's no project.
2121
/// </summary>
22-
public string ProjectDirectory { get; }
22+
public string AppHostDirectory { get; }
2323

2424
/// <inheritdoc cref="HostApplicationBuilder.Environment" />
2525
public IHostEnvironment Environment { get; }

src/Aspire.Hosting/IServiceMetadata.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ namespace Aspire.Hosting;
1010
/// </summary>
1111
public interface IServiceMetadata : IResourceAnnotation
1212
{
13-
/// <summary>
14-
/// Gets the name of the assembly containing the service.
15-
/// </summary>
16-
public string AssemblyName { get; }
17-
18-
/// <summary>
19-
/// Gets the fully-qualified path to the assembly containing the service.
20-
/// </summary>
21-
public string AssemblyPath { get; }
22-
2313
/// <summary>
2414
/// Gets the fully-qualified path to the project containing the service.
2515
/// </summary>
@@ -28,9 +18,5 @@ public interface IServiceMetadata : IResourceAnnotation
2818

2919
internal class ServiceMetadata(string projectPath) : IServiceMetadata
3020
{
31-
public string AssemblyName => throw new NotImplementedException();
32-
33-
public string AssemblyPath => throw new NotImplementedException();
34-
3521
public string ProjectPath { get; } = projectPath;
3622
}

src/Aspire.Hosting/build/Aspire.Hosting.targets

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ namespace ]]>%(Namespace)<![CDATA[%3B
3838
3939
public class ]]>%(ClassName)<![CDATA[ : IServiceMetadata
4040
{
41-
public string AssemblyName => """]]>%(AssemblyName)<![CDATA["""%3B
42-
public string AssemblyPath => """]]>%(Identity)<![CDATA["""%3B
4341
public string ProjectPath => """]]>%(ProjectPath)<![CDATA["""%3B
4442
}]]>
4543
</Source>

tests/Aspire.Hosting.Tests/DistributedApplicationBuilderTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Aspire.Hosting.Lifecycle;
55
using Aspire.Hosting.Publishing;
6+
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Options;
89
using Xunit;
@@ -83,6 +84,17 @@ public void BuilderConfiguresPublishingOptionsFromConfig()
8384
Assert.Equal("/path/", publishOptions.Value.OutputPath);
8485
}
8586

87+
[Fact]
88+
public void AppHostDirectoryAvailableViaConfig()
89+
{
90+
var appBuilder = DistributedApplication.CreateBuilder();
91+
var appHostDirectory = appBuilder.AppHostDirectory;
92+
var app = appBuilder.Build();
93+
94+
var config = app.Services.GetRequiredService<IConfiguration>();
95+
Assert.Equal(appHostDirectory, config["AppHost:Directory"]);
96+
}
97+
8698
private sealed class TestResource : IResource
8799
{
88100
public string Name => throw new NotImplementedException();

tests/Aspire.Hosting.Tests/ProjectResourceTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ private IDistributedApplicationBuilder CreateBuilder()
161161

162162
private sealed class TestProject : IServiceMetadata
163163
{
164-
public string AssemblyName => "someapp.dll";
165-
166-
public string AssemblyPath => "non-exist-path";
167-
168164
public string ProjectPath => "another-path";
169165
}
170166
}

0 commit comments

Comments
 (0)