From c7d3c1f36aef954c6b32a7cd6f8e4dba672d9cbe Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Thu, 19 Mar 2020 09:50:39 -0700 Subject: [PATCH] Use custom DebugProxyHost to initialize DebugProxy config (#19980) Addresses #19909 --- .../DebugProxy/src/Hosting/DebugProxyHost.cs | 63 +++++++++++++++++++ .../WebAssembly/DebugProxy/src/Program.cs | 26 +------- .../Server/src/DebugProxyLauncher.cs | 15 ----- 3 files changed, 66 insertions(+), 38 deletions(-) create mode 100644 src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs diff --git a/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs b/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs new file mode 100644 index 0000000000..8a710028a0 --- /dev/null +++ b/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs @@ -0,0 +1,63 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Microsoft.AspNetCore.Components.WebAssembly.DebugProxy.Hosting +{ + public static class DebugProxyHost + { + /// + /// Creates a custom HostBuilder for the DebugProxyLauncher so that we can inject + /// only the needed configurations. + /// + /// Command line arguments passed in + /// Host where browser is listening for debug connections + /// + public static IHostBuilder CreateDefaultBuilder(string[] args, string browserHost) + { + var builder = new HostBuilder(); + + builder.ConfigureAppConfiguration((hostingContext, config) => + { + if (args != null) + { + config.AddCommandLine(args); + } + config.AddJsonFile("blazor-debugproxysettings.json", optional: true, reloadOnChange: true); + }) + .ConfigureLogging((hostingContext, logging) => + { + logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); + logging.AddConsole(); + logging.AddDebug(); + logging.AddEventSourceLogger(); + }) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + + // By default we bind to a dyamic port + // This can be overridden using an option like "--urls http://localhost:9500" + webBuilder.UseUrls("http://127.0.0.1:0"); + }) + .ConfigureServices(serviceCollection => + { + serviceCollection.AddSingleton(new DebugProxyOptions + { + BrowserHost = browserHost + }); + }); + + return builder; + + } + } +} diff --git a/src/Components/WebAssembly/DebugProxy/src/Program.cs b/src/Components/WebAssembly/DebugProxy/src/Program.cs index 0507d82130..ce67276e6e 100644 --- a/src/Components/WebAssembly/DebugProxy/src/Program.cs +++ b/src/Components/WebAssembly/DebugProxy/src/Program.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using Microsoft.AspNetCore.Components.WebAssembly.DebugProxy.Hosting; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.CommandLineUtils; using Microsoft.Extensions.Configuration; @@ -36,29 +37,8 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.DebugProxy app.OnExecute(() => { - var host = Host.CreateDefaultBuilder(args) - .ConfigureAppConfiguration((hostingContext, config) => - { - config.AddCommandLine(args); - }) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - - // By default we bind to a dyamic port - // This can be overridden using an option like "--urls http://localhost:9500" - webBuilder.UseUrls("http://127.0.0.1:0"); - }) - .ConfigureServices(serviceCollection => - { - serviceCollection.AddSingleton(new DebugProxyOptions - { - BrowserHost = browserHostOption.HasValue() - ? browserHostOption.Value() - : "http://127.0.0.1:9222", - }); - }) - .Build(); + var browserHost = browserHostOption.HasValue() ? browserHostOption.Value(): "http://127.0.0.1:9222"; + var host = DebugProxyHost.CreateDefaultBuilder(args, browserHost).Build(); if (ownerPidOption.HasValue()) { diff --git a/src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs b/src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs index fc55e94aec..bdbd662b12 100644 --- a/src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs +++ b/src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs @@ -52,7 +52,6 @@ namespace Microsoft.AspNetCore.Builder UseShellExecute = false, RedirectStandardOutput = true, }; - RemoveUnwantedEnvironmentVariables(processStartInfo.Environment); var debugProxyProcess = Process.Start(processStartInfo); CompleteTaskWhenServerIsReady(debugProxyProcess, tcs); @@ -65,20 +64,6 @@ namespace Microsoft.AspNetCore.Builder return await tcs.Task; } - private static void RemoveUnwantedEnvironmentVariables(IDictionary environment) - { - // Generally we expect to pass through most environment variables, since dotnet might - // need them for arbitrary reasons to function correctly. However, we specifically don't - // want to pass through any ASP.NET Core hosting related ones, since the child process - // shouldn't be trying to use the same port numbers, etc. In particular we need to break - // the association with IISExpress and the MS-ASPNETCORE-TOKEN check. - var keysToRemove = environment.Keys.Where(key => key.StartsWith("ASPNETCORE_")).ToList(); - foreach (var key in keysToRemove) - { - environment.Remove(key); - } - } - private static string LocateDebugProxyExecutable(IWebHostEnvironment environment) { var assembly = Assembly.Load(environment.ApplicationName);