Skip to content

Commit e0967c6

Browse files
committed
PR feedback + logging
1 parent 96423e2 commit e0967c6

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

src/Tools/dotnet-watch/src/BrowserRefreshServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private async Task WebSocketRequest(HttpContext context)
102102
await _taskCompletionSource.Task;
103103
}
104104

105-
public async void SendMessage(byte[] messageBytes)
105+
public async Task SendMessage(byte[] messageBytes)
106106
{
107107
if (_webSocket == null || _webSocket.CloseStatus.HasValue)
108108
{
@@ -123,7 +123,7 @@ public async ValueTask DisposeAsync()
123123
{
124124
if (_webSocket != null)
125125
{
126-
await _webSocket.CloseOutputAsync(WebSocketCloseStatus.Empty, null, default);
126+
await _webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default);
127127
_webSocket.Dispose();
128128
}
129129

src/Tools/dotnet-watch/src/LaunchBrowserFilter.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public LaunchBrowserFilter()
4040
_browserPath = Environment.GetEnvironmentVariable("DOTNET_WATCH_BROWSER_PATH");
4141
}
4242

43-
public ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken cancellationToken)
43+
public async ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken cancellationToken)
4444
{
4545
if (_suppressLaunchBrowser)
4646
{
47-
return default;
47+
return;
4848
}
4949

5050
if (context.Iteration == 0)
@@ -53,6 +53,7 @@ public ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken canc
5353

5454
if (CanLaunchBrowser(context, out var launchPath))
5555
{
56+
context.Reporter.Verbose("dotnet-watch is configured to launch a browser on ASP.NET Core application startup.");
5657
_canLaunchBrowser = true;
5758
_launchPath = launchPath;
5859

@@ -71,11 +72,9 @@ public ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken canc
7172
if (context.Iteration > 0)
7273
{
7374
// We've detected a change. Notify the browser.
74-
_refreshServer.SendMessage(WaitMessage);
75+
await _refreshServer.SendMessage(WaitMessage);
7576
}
7677
}
77-
78-
return default;
7978
}
8079

8180
private void OnOutput(object sender, DataReceivedEventArgs eventArgs)
@@ -117,7 +116,7 @@ private void OnOutput(object sender, DataReceivedEventArgs eventArgs)
117116
else
118117
{
119118
_reporter.Verbose($"Reloading browser");
120-
_refreshServer.SendMessage(ReloadMessage);
119+
_ = _refreshServer.SendMessage(ReloadMessage);
121120
}
122121
}
123122
}
@@ -150,31 +149,34 @@ private void LaunchBrowser(string launchUrl)
150149
private static bool CanLaunchBrowser(DotNetWatchContext context, out string launchUrl)
151150
{
152151
launchUrl = null;
152+
var reporter = context.Reporter;
153153

154154
if (!context.FileSet.IsNetCoreApp31OrNewer)
155155
{
156156
// Browser refresh middleware supports 3.1 or newer
157+
reporter.Verbose("Browser refresh is only supported in .NET Core 3.1 or newer projects.");
157158
return false;
158159
}
159160

160161
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
161162
{
162163
// Launching a browser requires file associations that are not available in all operating systems.
164+
reporter.Verbose("Browser refresh is only supported in Windows and MacOSX.");
163165
return false;
164166
}
165167

166-
var reporter = context.Reporter;
167-
168168
var dotnetCommand = context.ProcessSpec.Arguments.FirstOrDefault();
169169
if (!string.Equals(dotnetCommand, "run", StringComparison.Ordinal))
170170
{
171+
reporter.Verbose("Browser refresh is only supported for run commands.");
171172
return false;
172173
}
173174

174175
// We're executing the run-command. Determine if the launchSettings allows it
175176
var launchSettingsPath = Path.Combine(context.ProcessSpec.WorkingDirectory, "Properties", "launchSettings.json");
176177
if (!File.Exists(launchSettingsPath))
177178
{
179+
reporter.Verbose($"No launchSettings.json file found at {launchSettingsPath}. Unable to determine if browser refresh is allowed.");
178180
return false;
179181
}
180182

@@ -185,19 +187,27 @@ private static bool CanLaunchBrowser(DotNetWatchContext context, out string laun
185187
File.ReadAllText(launchSettingsPath),
186188
new JsonSerializerOptions(JsonSerializerDefaults.Web));
187189
}
188-
catch
190+
catch (Exception ex)
189191
{
192+
reporter.Verbose($"Error reading launchSettings.json: {ex}.");
190193
return false;
191194
}
192195

193196
var defaultProfile = launchSettings.Profiles.FirstOrDefault(f => f.Value.CommandName == "Project").Value;
194197
if (defaultProfile is null)
195198
{
199+
reporter.Verbose("Unable to find default launchSettings profile.");
200+
return false;
201+
}
202+
203+
if (!defaultProfile.LaunchBrowser)
204+
{
205+
reporter.Verbose("launchSettings does not allow launching browsers.");
196206
return false;
197207
}
198208

199209
launchUrl = defaultProfile.LaunchUrl;
200-
return defaultProfile.LaunchBrowser;
210+
return true;
201211
}
202212

203213
public async ValueTask DisposeAsync()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:38420",
8+
"sslPort": 44393
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "weatherforecast",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"b3": {
21+
"commandName": "Project",
22+
"launchBrowser": true,
23+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
24+
"environmentVariables": {
25+
"ASPNETCORE_ENVIRONMENT": "Development"
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)