Skip to content

Use custom DebugProxyHost to initialize DebugProxy config #19980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

}
}
}
26 changes: 3 additions & 23 deletions src/Components/WebAssembly/DebugProxy/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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())
{
Expand Down
15 changes: 0 additions & 15 deletions src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ private static async Task<string> LaunchAndGetUrl(IServiceProvider serviceProvid
UseShellExecute = false,
RedirectStandardOutput = true,
};
RemoveUnwantedEnvironmentVariables(processStartInfo.Environment);

var debugProxyProcess = Process.Start(processStartInfo);
CompleteTaskWhenServerIsReady(debugProxyProcess, tcs);
Expand All @@ -65,20 +64,6 @@ private static async Task<string> LaunchAndGetUrl(IServiceProvider serviceProvid
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);
Expand Down