Skip to content

Commit c7d3c1f

Browse files
authored
Use custom DebugProxyHost to initialize DebugProxy config (#19980)
Addresses #19909
1 parent 4339397 commit c7d3c1f

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Hosting;
11+
using Microsoft.Extensions.Logging;
12+
13+
namespace Microsoft.AspNetCore.Components.WebAssembly.DebugProxy.Hosting
14+
{
15+
public static class DebugProxyHost
16+
{
17+
/// <summary>
18+
/// Creates a custom HostBuilder for the DebugProxyLauncher so that we can inject
19+
/// only the needed configurations.
20+
/// </summary>
21+
/// <param name="args">Command line arguments passed in</param>
22+
/// <param name="browserHost">Host where browser is listening for debug connections</param>
23+
/// <returns><see cref="IHostBuilder"></returns>
24+
public static IHostBuilder CreateDefaultBuilder(string[] args, string browserHost)
25+
{
26+
var builder = new HostBuilder();
27+
28+
builder.ConfigureAppConfiguration((hostingContext, config) =>
29+
{
30+
if (args != null)
31+
{
32+
config.AddCommandLine(args);
33+
}
34+
config.AddJsonFile("blazor-debugproxysettings.json", optional: true, reloadOnChange: true);
35+
})
36+
.ConfigureLogging((hostingContext, logging) =>
37+
{
38+
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
39+
logging.AddConsole();
40+
logging.AddDebug();
41+
logging.AddEventSourceLogger();
42+
})
43+
.ConfigureWebHostDefaults(webBuilder =>
44+
{
45+
webBuilder.UseStartup<Startup>();
46+
47+
// By default we bind to a dyamic port
48+
// This can be overridden using an option like "--urls http://localhost:9500"
49+
webBuilder.UseUrls("http://127.0.0.1:0");
50+
})
51+
.ConfigureServices(serviceCollection =>
52+
{
53+
serviceCollection.AddSingleton(new DebugProxyOptions
54+
{
55+
BrowserHost = browserHost
56+
});
57+
});
58+
59+
return builder;
60+
61+
}
62+
}
63+
}

src/Components/WebAssembly/DebugProxy/src/Program.cs

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

44
using System;
55
using System.Diagnostics;
6+
using Microsoft.AspNetCore.Components.WebAssembly.DebugProxy.Hosting;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.Extensions.CommandLineUtils;
89
using Microsoft.Extensions.Configuration;
@@ -36,29 +37,8 @@ static int Main(string[] args)
3637

3738
app.OnExecute(() =>
3839
{
39-
var host = Host.CreateDefaultBuilder(args)
40-
.ConfigureAppConfiguration((hostingContext, config) =>
41-
{
42-
config.AddCommandLine(args);
43-
})
44-
.ConfigureWebHostDefaults(webBuilder =>
45-
{
46-
webBuilder.UseStartup<Startup>();
47-
48-
// By default we bind to a dyamic port
49-
// This can be overridden using an option like "--urls http://localhost:9500"
50-
webBuilder.UseUrls("http://127.0.0.1:0");
51-
})
52-
.ConfigureServices(serviceCollection =>
53-
{
54-
serviceCollection.AddSingleton(new DebugProxyOptions
55-
{
56-
BrowserHost = browserHostOption.HasValue()
57-
? browserHostOption.Value()
58-
: "http://127.0.0.1:9222",
59-
});
60-
})
61-
.Build();
40+
var browserHost = browserHostOption.HasValue() ? browserHostOption.Value(): "http://127.0.0.1:9222";
41+
var host = DebugProxyHost.CreateDefaultBuilder(args, browserHost).Build();
6242

6343
if (ownerPidOption.HasValue())
6444
{

src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ private static async Task<string> LaunchAndGetUrl(IServiceProvider serviceProvid
5252
UseShellExecute = false,
5353
RedirectStandardOutput = true,
5454
};
55-
RemoveUnwantedEnvironmentVariables(processStartInfo.Environment);
5655

5756
var debugProxyProcess = Process.Start(processStartInfo);
5857
CompleteTaskWhenServerIsReady(debugProxyProcess, tcs);
@@ -65,20 +64,6 @@ private static async Task<string> LaunchAndGetUrl(IServiceProvider serviceProvid
6564
return await tcs.Task;
6665
}
6766

68-
private static void RemoveUnwantedEnvironmentVariables(IDictionary<string, string> environment)
69-
{
70-
// Generally we expect to pass through most environment variables, since dotnet might
71-
// need them for arbitrary reasons to function correctly. However, we specifically don't
72-
// want to pass through any ASP.NET Core hosting related ones, since the child process
73-
// shouldn't be trying to use the same port numbers, etc. In particular we need to break
74-
// the association with IISExpress and the MS-ASPNETCORE-TOKEN check.
75-
var keysToRemove = environment.Keys.Where(key => key.StartsWith("ASPNETCORE_")).ToList();
76-
foreach (var key in keysToRemove)
77-
{
78-
environment.Remove(key);
79-
}
80-
}
81-
8267
private static string LocateDebugProxyExecutable(IWebHostEnvironment environment)
8368
{
8469
var assembly = Assembly.Load(environment.ApplicationName);

0 commit comments

Comments
 (0)