public async ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken cancellationToken) { if (_suppressLaunchBrowser) { return; } if (context.Iteration == 0) { _reporter = context.Reporter; if (CanLaunchBrowser(context, out var launchPath)) { context.Reporter.Verbose("dotnet-watch is configured to launch a browser on ASP.NET Core application startup."); _canLaunchBrowser = true; _launchPath = launchPath; _cancellationToken = cancellationToken; // We've redirected the output, but want to ensure that it continues to appear in the user's console. context.ProcessSpec.OnOutput += (_, eventArgs) => Console.WriteLine(eventArgs.Data); context.ProcessSpec.OnOutput += OnOutput; if (!_suppressBrowserRefresh) { _refreshServer = new BrowserRefreshServer(context.Reporter); var serverUrl = await _refreshServer.StartAsync(cancellationToken); context.BrowserRefreshServer = _refreshServer; context.Reporter.Verbose($"Refresh server running at {serverUrl}."); context.ProcessSpec.EnvironmentVariables["ASPNETCORE_AUTO_RELOAD_WS_ENDPOINT"] = serverUrl; var pathToMiddleware = Path.Combine(AppContext.BaseDirectory, "middleware", "Microsoft.AspNetCore.Watch.BrowserRefresh.dll"); context.ProcessSpec.EnvironmentVariables["DOTNET_STARTUP_HOOKS"] = pathToMiddleware; context.ProcessSpec.EnvironmentVariables["ASPNETCORE_HOSTINGSTARTUPASSEMBLIES"] = "Microsoft.AspNetCore.Watch.BrowserRefresh"; } } } if (_canLaunchBrowser) { if (context.Iteration > 0) { // We've detected a change. Notify the browser. await(_refreshServer?.SendWaitMessageAsync(cancellationToken) ?? default); } } }
public async ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken cancellationToken) { if (_suppressBrowserRefresh) { return; } if (context.Iteration == 0) { if (context.ProjectGraph is null) { _reporter.Verbose("Unable to determine if this project is a webapp."); return; } else if (IsWebApp(context.ProjectGraph)) { _reporter.Verbose("Configuring the app to use browser-refresh middleware."); } else { _reporter.Verbose("Skipping configuring browser-refresh middleware since this is not a webapp."); return; } _refreshServer = new BrowserRefreshServer(context.Reporter); context.BrowserRefreshServer = _refreshServer; var serverUrls = string.Join(',', await _refreshServer.StartAsync(cancellationToken)); context.Reporter.Verbose($"Refresh server running at {serverUrls}."); context.ProcessSpec.EnvironmentVariables["ASPNETCORE_AUTO_RELOAD_WS_ENDPOINT"] = serverUrls; context.ProcessSpec.EnvironmentVariables["ASPNETCORE_AUTO_RELOAD_WS_KEY"] = _refreshServer.ServerKey; var pathToMiddleware = Path.Combine(AppContext.BaseDirectory, "middleware", "Microsoft.AspNetCore.Watch.BrowserRefresh.dll"); context.ProcessSpec.EnvironmentVariables.DotNetStartupHooks.Add(pathToMiddleware); context.ProcessSpec.EnvironmentVariables.AspNetCoreHostingStartupAssemblies.Add("Microsoft.AspNetCore.Watch.BrowserRefresh"); } else if (!_suppressBrowserRefresh) { // We've detected a change. Notify the browser. await(_refreshServer?.SendWaitMessageAsync(cancellationToken) ?? default); } }