@@ -40,11 +40,11 @@ public LaunchBrowserFilter()
40
40
_browserPath = Environment . GetEnvironmentVariable ( "DOTNET_WATCH_BROWSER_PATH" ) ;
41
41
}
42
42
43
- public ValueTask ProcessAsync ( DotNetWatchContext context , CancellationToken cancellationToken )
43
+ public async ValueTask ProcessAsync ( DotNetWatchContext context , CancellationToken cancellationToken )
44
44
{
45
45
if ( _suppressLaunchBrowser )
46
46
{
47
- return default ;
47
+ return ;
48
48
}
49
49
50
50
if ( context . Iteration == 0 )
@@ -53,6 +53,7 @@ public ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken canc
53
53
54
54
if ( CanLaunchBrowser ( context , out var launchPath ) )
55
55
{
56
+ context . Reporter . Verbose ( "dotnet-watch is configured to launch a browser on ASP.NET Core application startup." ) ;
56
57
_canLaunchBrowser = true ;
57
58
_launchPath = launchPath ;
58
59
@@ -71,11 +72,9 @@ public ValueTask ProcessAsync(DotNetWatchContext context, CancellationToken canc
71
72
if ( context . Iteration > 0 )
72
73
{
73
74
// We've detected a change. Notify the browser.
74
- _refreshServer . SendMessage ( WaitMessage ) ;
75
+ await _refreshServer . SendMessage ( WaitMessage ) ;
75
76
}
76
77
}
77
-
78
- return default ;
79
78
}
80
79
81
80
private void OnOutput ( object sender , DataReceivedEventArgs eventArgs )
@@ -117,7 +116,7 @@ private void OnOutput(object sender, DataReceivedEventArgs eventArgs)
117
116
else
118
117
{
119
118
_reporter . Verbose ( $ "Reloading browser") ;
120
- _refreshServer . SendMessage ( ReloadMessage ) ;
119
+ _ = _refreshServer . SendMessage ( ReloadMessage ) ;
121
120
}
122
121
}
123
122
}
@@ -150,31 +149,34 @@ private void LaunchBrowser(string launchUrl)
150
149
private static bool CanLaunchBrowser ( DotNetWatchContext context , out string launchUrl )
151
150
{
152
151
launchUrl = null ;
152
+ var reporter = context . Reporter ;
153
153
154
154
if ( ! context . FileSet . IsNetCoreApp31OrNewer )
155
155
{
156
156
// Browser refresh middleware supports 3.1 or newer
157
+ reporter . Verbose ( "Browser refresh is only supported in .NET Core 3.1 or newer projects." ) ;
157
158
return false ;
158
159
}
159
160
160
161
if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) && ! RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
161
162
{
162
163
// 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." ) ;
163
165
return false ;
164
166
}
165
167
166
- var reporter = context . Reporter ;
167
-
168
168
var dotnetCommand = context . ProcessSpec . Arguments . FirstOrDefault ( ) ;
169
169
if ( ! string . Equals ( dotnetCommand , "run" , StringComparison . Ordinal ) )
170
170
{
171
+ reporter . Verbose ( "Browser refresh is only supported for run commands." ) ;
171
172
return false ;
172
173
}
173
174
174
175
// We're executing the run-command. Determine if the launchSettings allows it
175
176
var launchSettingsPath = Path . Combine ( context . ProcessSpec . WorkingDirectory , "Properties" , "launchSettings.json" ) ;
176
177
if ( ! File . Exists ( launchSettingsPath ) )
177
178
{
179
+ reporter . Verbose ( "Browser refresh is only supported is launchSettings allows it. No launchSettings.json file found." ) ;
178
180
return false ;
179
181
}
180
182
@@ -185,19 +187,27 @@ private static bool CanLaunchBrowser(DotNetWatchContext context, out string laun
185
187
File . ReadAllText ( launchSettingsPath ) ,
186
188
new JsonSerializerOptions ( JsonSerializerDefaults . Web ) ) ;
187
189
}
188
- catch
190
+ catch ( Exception ex )
189
191
{
192
+ reporter . Verbose ( $ "Error reading launchSettings.json: { ex } .") ;
190
193
return false ;
191
194
}
192
195
193
196
var defaultProfile = launchSettings . Profiles . FirstOrDefault ( f => f . Value . CommandName == "Project" ) . Value ;
194
197
if ( defaultProfile is null )
195
198
{
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." ) ;
196
206
return false ;
197
207
}
198
208
199
209
launchUrl = defaultProfile . LaunchUrl ;
200
- return defaultProfile . LaunchBrowser ;
210
+ return true ;
201
211
}
202
212
203
213
public async ValueTask DisposeAsync ( )
0 commit comments