public async Task ListenToSingleRequestAndRespondAsync()

in src/PowerShell/Network/HttpListenerInterceptor.cs [24:66]


        public async Task<Uri> ListenToSingleRequestAndRespondAsync(int port, Func<Uri, MessageAndHttpCode> responseProducer, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            HttpListener httpListener = null;

            try
            {
                string urlToListenTo = $"http://localhost:{port}/";

                httpListener = new HttpListener();
                httpListener.Prefixes.Add(urlToListenTo);

                httpListener.Start();
                ServiceClientTracing.Information($"[HttpListenerInterceptor] Listening for authorization code on {urlToListenTo}");

                using (cancellationToken.Register(() =>
                {
                    TryStopListening(httpListener);
                }))
                {
                    HttpListenerContext context = await httpListener.GetContextAsync().ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();

                    Respond(responseProducer, context);
                    ServiceClientTracing.Information($"[HttpListenerInterceptor] Received a message on {urlToListenTo}");

                    // the request URL should now contain the auth code and pkce
                    return context.Request.Url;
                }
            }
            catch (ObjectDisposedException)
            {
                cancellationToken.ThrowIfCancellationRequested();

                throw;
            }
            finally
            {
                TryStopListening(httpListener);
            }
        }