Skip to content

Commit 1a41af8

Browse files
committed
Usings and ProcessUtilities
1 parent 77a88ef commit 1a41af8

13 files changed

+73
-32
lines changed

src/BuiltInTools/HotReloadAgent.Host/Microsoft.DotNet.HotReload.Agent.Host.Package.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
<Import Project="..\HotReloadAgent.Data\Microsoft.DotNet.HotReload.Agent.Data.projitems" Label="Shared" />
3636
<Import Project="..\HotReloadAgent.PipeRpc\Microsoft.DotNet.HotReload.Agent.PipeRpc.projitems" Label="Shared" />
3737

38+
<!-- Make sure the shared source files do not require any global usings -->
3839
<ItemGroup>
39-
<Compile Include="..\dotnet-watch\Utilities\ProcessUtilities.cs" />
40+
<Using Remove="@(Using)" />
4041
</ItemGroup>
4142
</Project>

src/BuiltInTools/HotReloadAgent.Host/Microsoft.DotNet.HotReload.Agent.Host.projitems

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)**\*.cs" />
13-
<Compile Include="$(MSBuildThisFileDirectory)..\dotnet-watch\Utilities\ProcessUtilities.cs" Link="ProcessUtilities.cs" />
1413
</ItemGroup>
1514
</Project>

src/BuiltInTools/HotReloadAgent.Host/PipeListener.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
#nullable enable
55

6+
using System;
67
using System.Diagnostics;
78
using System.IO.Pipes;
89
using System.Reflection;
910
using System.Runtime.Loader;
11+
using System.Threading;
12+
using System.Threading.Tasks;
1013

1114
namespace Microsoft.DotNet.HotReload;
1215

src/BuiltInTools/HotReloadAgent.Host/StartupHook.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
#nullable enable
55

6+
using System;
67
using System.Diagnostics;
8+
using System.IO;
79
using System.IO.Pipes;
10+
using System.Linq;
811
using System.Reflection;
12+
using System.Runtime.InteropServices;
913
using System.Runtime.Loader;
14+
using System.Threading;
15+
using System.Threading.Tasks;
1016
using Microsoft.DotNet.HotReload;
11-
using Microsoft.DotNet.Watch;
1217

1318
/// <summary>
1419
/// The runtime startup hook looks for top-level type named "StartupHook".
@@ -94,7 +99,27 @@ private static void RegisterSignalHandlers()
9499
{
95100
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
96101
{
97-
ProcessUtilities.EnableWindowsCtrlCHandling(Log);
102+
// Enables handling of Ctrl+C in a process where it was disabled.
103+
//
104+
// If a process is launched with CREATE_NEW_PROCESS_GROUP flag
105+
// it allows the parent process to send Ctrl+C event to the child process,
106+
// but also disables Ctrl+C handlers.
107+
//
108+
// "If the HandlerRoutine parameter is NULL, a TRUE value causes the calling process to ignore CTRL+C input,
109+
// and a FALSE value restores normal processing of CTRL+C input.
110+
// This attribute of ignoring or processing CTRL+C is inherited by child processes."
111+
112+
if (SetConsoleCtrlHandler(null, false))
113+
{
114+
Log("Windows Ctrl+C handling enabled.");
115+
}
116+
else
117+
{
118+
Log($"Failed to enable Ctrl+C handling: {GetLastPInvokeErrorMessage()}");
119+
}
120+
121+
[DllImport("kernel32.dll", SetLastError = true)]
122+
static extern bool SetConsoleCtrlHandler(Delegate? handler, bool add);
98123
}
99124
else
100125
{
@@ -120,6 +145,16 @@ private static void RegisterSignalHandlers()
120145
}
121146
}
122147

148+
private static string GetLastPInvokeErrorMessage()
149+
{
150+
var error = Marshal.GetLastPInvokeError();
151+
#if NET10_0_OR_GREATER
152+
return $"{Marshal.GetPInvokeErrorMessage(error)} (code {error})";
153+
#else
154+
return $"error code {error}";
155+
#endif
156+
}
157+
123158
private static void Log(string message)
124159
{
125160
var prefix = s_standardOutputLogPrefix;

src/BuiltInTools/Web.Middleware/BlazorWasmHotReloadMiddleware.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
#nullable enable
55

6+
using System.Collections.Generic;
67
using System.Text.Json;
8+
using System.Threading.Tasks;
79
using Microsoft.AspNetCore.Http;
810
using Microsoft.Extensions.Logging;
911

src/BuiltInTools/Web.Middleware/BrowserRefreshMiddleware.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#nullable enable
55

6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Threading.Tasks;
610
using Microsoft.AspNetCore.Http;
711
using Microsoft.AspNetCore.Http.Features;
812
using Microsoft.Extensions.Logging;

src/BuiltInTools/Web.Middleware/BrowserScriptMiddleware.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
#nullable enable
55

6+
using System;
67
using System.Globalization;
8+
using System.IO;
9+
using System.Text;
10+
using System.Threading.Tasks;
711
using Microsoft.AspNetCore.Http;
812
using Microsoft.Extensions.Logging;
913

src/BuiltInTools/Web.Middleware/HostingStartup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#nullable enable
55

6+
using System;
7+
using System.Threading.Tasks;
68
using Microsoft.AspNetCore.Builder;
79
using Microsoft.AspNetCore.Hosting;
810
using Microsoft.Extensions.DependencyInjection;

src/BuiltInTools/Web.Middleware/Microsoft.DotNet.HotReload.Web.Middleware.Package.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@
3333
<FrameworkReference Update="Microsoft.AspNetCore.App" TargetingPackVersion="6.0.2" />
3434
<FrameworkReference Update="Microsoft.NETCore.App" TargetingPackVersion="6.0.0" />
3535
</ItemGroup>
36+
37+
<!-- Make sure the shared source files do not require any global usings -->
38+
<ItemGroup>
39+
<Using Remove="@(Using)" />
40+
</ItemGroup>
3641
</Project>

src/BuiltInTools/Web.Middleware/ResponseStreamWrapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
#nullable enable
55

6+
using System;
67
using System.Diagnostics;
8+
using System.IO;
79
using System.IO.Compression;
810
using System.IO.Pipelines;
11+
using System.Linq;
12+
using System.Threading;
13+
using System.Threading.Tasks;
914
using Microsoft.AspNetCore.Http;
1015
using Microsoft.Extensions.Logging;
1116
using Microsoft.Net.Http.Headers;

0 commit comments

Comments
 (0)