mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-11-28 11:22:50 +01:00
Switch to IHost for .NET 10 + update dotnet-WireMock (#1532)
* Switch to IHost for .NET 10 * 'dotnet-WireMock stops on CTRL-C' * Potential fix for pull request finding 'Missing Dispose call on local IDisposable' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * . --------- Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
f594a7cb19
commit
2760ecbe00
3 files changed
+164
-49
No files matched your search
@@ -1,7 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Net.StandAlone;
|
||||
@@ -25,6 +23,7 @@ public class Program
|
||||
private static readonly IWireMockLogger Logger = new WireMockLogger(XLogger);
|
||||
|
||||
private static WireMockServer _server = null!;
|
||||
private static int _stopped;
|
||||
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
@@ -33,29 +32,59 @@ public class Program
|
||||
return;
|
||||
}
|
||||
|
||||
using var cancelationTokenSource = new CancellationTokenSource();
|
||||
|
||||
Logger.Info("Press Ctrl+C to shut down");
|
||||
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
Stop("CancelKeyPress");
|
||||
// Prevent the runtime from terminating the process (exit code 130 / -1073741510)
|
||||
// Main stops the server and returns normally.
|
||||
e.Cancel = true;
|
||||
Logger.Info("Shutdown requested because 'CancelKeyPress'");
|
||||
cancelationTokenSource.Cancel();
|
||||
};
|
||||
|
||||
System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx =>
|
||||
{
|
||||
Stop("AssemblyLoadContext.Default.Unloading");
|
||||
Stop("AssemblyLoadContext.Default.Unloading", cancelationTokenSource);
|
||||
};
|
||||
|
||||
while (true)
|
||||
while (!cancelationTokenSource.IsCancellationRequested)
|
||||
{
|
||||
Logger.Info("Server running : {0}", _server.IsStarted);
|
||||
await Task.Delay(SleepTime).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Delay(SleepTime, cancelationTokenSource.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Shutdown requested
|
||||
}
|
||||
}
|
||||
|
||||
Stop("shutdown requested", cancelationTokenSource);
|
||||
}
|
||||
|
||||
private static void Stop(string why)
|
||||
private static void Stop(string why, CancellationTokenSource cancellationTokenSource)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _stopped, 1) == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Logger.Info("Server stopping because '{0}'", why);
|
||||
_server.Stop();
|
||||
Logger.Info("Server stopped");
|
||||
|
||||
try
|
||||
{
|
||||
cancellationTokenSource.Cancel();
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// Main already finished
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user