Use custom DebugProxyHost to initialize DebugProxy config (#19980)
Addresses #19909
This commit is contained in:
parent
4339397eb0
commit
c7d3c1f36a
|
|
@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue