Skip to content

Instantly share code, notes, and snippets.

@lostmsu
Created September 23, 2024 14:46
Show Gist options
  • Save lostmsu/e4e187200553c459ea24e8b03f3e1754 to your computer and use it in GitHub Desktop.
Save lostmsu/e4e187200553c459ea24e8b03f3e1754 to your computer and use it in GitHub Desktop.
#r "nuget: Microsoft.AspNetCore.App, 2.2.8"
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var log = app.Services.GetRequiredService<ILogger<WebApplication>>();
app.MapGet("/",
async (CancellationToken cancel) =>
{
try
{
await Task.Delay(TimeSpan.FromSeconds(60), cancel);
log.LogCritical("NOT CANCELLED!");
}
catch (OperationCanceledException)
{
log.LogError("Cancelled");
}
});
const string uri = "http://localhost:17825";
var http = new HttpClient();
new Thread(() =>
{
try
{
using var cancel = new CancellationTokenSource(TimeSpan.FromSeconds(1));
using var _ = http.GetAsync(uri, cancel.Token).Result;
}
catch { }
})
{
IsBackground = true,
}.Start();
app.Run(uri);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment