From c4cde3fc98ec17e058b247b2c2bd00fd03c39dc6 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 17 Mar 2020 17:05:44 -0700 Subject: [PATCH 1/2] Use custom DebugProxyHost to initialize DebugProxy config --- .../DebugProxy/src/Hosting/DebugProxyHost.cs | 64 +++++++++++++++++++ .../WebAssembly/DebugProxy/src/Program.cs | 26 +------- .../Server/src/DebugProxyLauncher.cs | 15 ----- 3 files changed, 67 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 000000000000..7a1d62be5e98 --- /dev/null +++ b/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs @@ -0,0 +1,64 @@ +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"); + }) + .UseDefaultServiceProvider((context, options) => + { + options.ValidateScopes = context.HostingEnvironment.IsDevelopment(); + }) + .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 0507d82130b0..ce67276e6e04 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 @@ static int Main(string[] args) 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 fc55e94aec0c..bdbd662b1237 100644 --- a/src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs +++ b/src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs @@ -52,7 +52,6 @@ private static async Task LaunchAndGetUrl(IServiceProvider serviceProvid UseShellExecute = false, RedirectStandardOutput = true, }; - RemoveUnwantedEnvironmentVariables(processStartInfo.Environment); var debugProxyProcess = Process.Start(processStartInfo); CompleteTaskWhenServerIsReady(debugProxyProcess, tcs); @@ -65,20 +64,6 @@ private static async Task LaunchAndGetUrl(IServiceProvider serviceProvid 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); From 9da89bc0d6488f604ab289554d0ae29297a977d7 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Thu, 19 Mar 2020 09:16:48 -0700 Subject: [PATCH 2/2] Respond to feedback from peer review --- .../WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs b/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs index 7a1d62be5e98..8a710028a0ca 100644 --- a/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs +++ b/src/Components/WebAssembly/DebugProxy/src/Hosting/DebugProxyHost.cs @@ -1,3 +1,6 @@ +// 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; @@ -45,10 +48,6 @@ public static IHostBuilder CreateDefaultBuilder(string[] args, string browserHos // This can be overridden using an option like "--urls http://localhost:9500" webBuilder.UseUrls("http://127.0.0.1:0"); }) - .UseDefaultServiceProvider((context, options) => - { - options.ValidateScopes = context.HostingEnvironment.IsDevelopment(); - }) .ConfigureServices(serviceCollection => { serviceCollection.AddSingleton(new DebugProxyOptions