I am writing a c# console app base on net core 3.1 linux
It was expected to
- run job async
- await job end
- catch the kill signal and do some clean job
here is my demo code:
namespace DeveloperHelper
{
public class Program
{
public static async Task Main(string[] args)
{
var http = new SimpleHttpServer();
var t = http.RunAsync();
Console.WriteLine("Now after http.RunAsync();");
AppDomain.CurrentDomain.UnhandledException += (s, e) => {
var ex = (Exception)e.ExceptionObject;
Console.WriteLine(ex.ToString());
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex));
};
AppDomain.CurrentDomain.ProcessExit += async (s, e) =>
{
Console.WriteLine("ProcessExit!");
await Task.Delay(new TimeSpan(0,0,1));
Console.WriteLine("ProcessExit! finished");
};
await Task.WhenAll(t);
}
}
public class SimpleHttpServer
{
private readonly HttpListener _httpListener;
public SimpleHttpServer()
{
_httpListener = new HttpListener();
_httpListener.Prefixes.Add("http://127.0.0.1:5100/");
}
public async Task RunAsync()
{
_httpListener.Start();
while (true)
{
Console.WriteLine("Now in while (true)");
var context = await _httpListener.GetContextAsync();
var response = context.Response;
const string rc = "{\"statusCode\":200, \"data\": true}";
var rbs = Encoding.UTF8.GetBytes(rc);
var st = response.OutputStream;
response.ContentType = "application/json";
response.StatusCode = 200;
await st.WriteAsync(rbs, 0, rbs.Length);
context.Response.Close();
}
}
}
}
expect it will print
Now in while (true)
Now after http.RunAsync();
ProcessExit!
ProcessExit! finished
but it only output
$ dotnet run
Now in while (true)
Now after http.RunAsync();
^C%
does the async/await block the kill signal to be watched by eventHandler?
the unexpected exception eventHandler do not have any output too.
is there any signal.signal(signal.SIGTERM, func)
in asp.net core?