How to prevent HttpListener from aborting pending requests on stoppage? HttpListener.Stop is not working
<p>I have a problem here. In below code the async/await pattern is used with HttpListener. When the request is sent via HTTP "delay" query string argument is expected and its value causes the server to delay the mentioned request processing for the given period. I need the server to process the pending requests even after the server stopped receiving new requests.</p>

<pre><code>static void Main(string[] args)
{
    HttpListener httpListener = new HttpListener();

    CountdownEvent sessions = new CountdownEvent(1);
    bool stopRequested = false;

    httpListener.Prefixes.Add("http://+:9000/GetData/");

    httpListener.Start();

    Task listenerTask = Task.Run(async () =&gt;
    {
        while (true)
        {
            try
            {
                var context = await httpListener.GetContextAsync();

                sessions.AddCount();

                Task childTask = Task.Run(async () =&gt;
                {
                    try
                    {
                        Console.WriteLine($"Request accepted: {context.Request.RawUrl}");

                        int delay = int.Parse(context.Request.QueryString["delay"]);

                        await Task.Delay(delay);

                        using (StreamWriter sw = new StreamWriter(context.Response.OutputStream, Encoding.Default, 4096, true))
                        {
                            await sw.WriteAsync("&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello world&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;");
                        }

                        context.Response.Close();
                    }
                    finally
                    {
                        sessions.Signal();
                    }
                });
            }
            catch (HttpListenerException ex)
            {
                if (stopRequested &amp;&amp; ex.ErrorCode == 995)
                {
                    break;
                }

                throw;
            }
        }
    });

    Console.WriteLine("Server is running. ENTER to stop...");

    Console.ReadLine();

    sessions.Signal();

    stopRequested = true;

    httpListener.Stop();

    Console.WriteLine("Stopped accepting requests. Waiting for the pendings...");

    listenerTask.Wait();

    sessions.Wait();

    Console.WriteLine("Finished");

    Console.ReadLine();

    httpListener.Close();
}
</code></pre>

<p>The exact problem here, is that when the server is stopped the HttpListener.Stop is called, but all the pending requests are aborted immediately, i.e. the code is unable to send the responses back.</p>

<p>In non-async/await pattern (i.e. simple Thread based implementation) I have a choice to abort the thread (which I suppose is very bad) and this will allow me to process pending requests, because this simply Aborts HttpListener.GetContext call.</p>

<p>Can you please point me out, what am I doing wrong and how to can I prevent HttpListener to abort pending requests in async/await pattern?</p>