Use custom DebugProxyHost to initialize DebugProxy config (#19980)

Addresses #19909
This commit is contained in:
Safia Abdalla 2020-03-19 09:50:39 -07:00 committed by GitHub
parent 4339397eb0
commit c7d3c1f36a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 38 deletions

View File

@ -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
{
/// <summary>
/// Creates a custom HostBuilder for the DebugProxyLauncher so that we can inject
/// only the needed configurations.
/// </summary>
/// <param name="args">Command line arguments passed in</param>
/// <param name="browserHost">Host where browser is listening for debug connections</param>
/// <returns><see cref="IHostBuilder"></returns>
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<Startup>();
// 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;
}
}
}

View File

@ -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<Startup>();
// 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())
{

View File

@ -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<string, string> 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);