public async Task <string> Listen(string id, CancellationToken cancel)
        {
            if (httpListener.IsListening)
            {
                httpListener.Stop();
            }
            httpListener.Start();

            try
            {
                using (cancel.Register(httpListener.Stop))
                {
                    while (true)
                    {
                        var context = await httpListener.GetContextAsync().ConfigureAwait(false);

                        var foo        = context.Request;
                        var queryParts = HttpUtility.ParseQueryString(context.Request.Url.Query);

                        if (queryParts["state"] == id)
                        {
                            context.Response.Close();
                            return(queryParts["code"]);
                        }
                    }
                }
            }
            finally
            {
                httpListener.Stop();
            }
        }
Example #2
0
        public async Task <string> Listen(string id, CancellationToken cancel)
        {
            if (httpListener.IsListening)
            {
                httpListener.Stop();
            }
            httpListener.Start();

            try
            {
                using (cancel.Register(httpListener.Stop))
                {
                    while (true)
                    {
                        lastContext = await httpListener.GetContextAsync().ConfigureAwait(false);

                        var queryParts = HttpUtility.ParseQueryString(lastContext.Request.Url.Query);

                        if (queryParts["state"] == id)
                        {
                            return(queryParts["code"]);
                        }
                    }
                }
            }
            catch (Exception)
            {
                httpListener.Stop();
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Await a new web request and process web request
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task ProcessRequestAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested && (_listener?.IsListening ?? false))
            {
                var context = await _listener.GetContextAsync(cancellationToken);

                Task.Run(() => DoHandleContextAsync(context));
            }
        }
Example #4
0
        public void Start()
        {
            if (IsListening || _starting)
            {
                return;
            }

            _starting = true;

            try
            {
                if (!_hasRun)
                {
                    _router.Initialize();
                    _httpListener.Prefixes.Add(_uriBuilder.Uri.ToString());
                }

                _httpListener.Start();
                _httpListenerTask = Task.Factory.StartNew(async() =>
                {
                    while (IsListening)
                    {
                        HttpContext context = await _httpListener.GetContextAsync();
                        _logger.Information("Received Request {0} {1}", context.Request.HttpMethod,
                                            context.Request.Path);
                        try
                        {
                            IResult result = _router.Route(context);
                            result.Execute(context);
                        }
                        catch (Exception ex)
                        {
                            context.Response.StatusCode = HttpStatusCode.InternalServerError;
                            context.Response.Send();
                            _logger.Error("Internal Server Error ({0}): {1}", ex.GetType(), ex.Message);
                        }
                    }
                }, TaskCreationOptions.LongRunning);
            }
            catch (Exception ex)
            {
                _logger.Error($"RestFulServer: {ex.Message}\r\n{ex.StackTrace}");
            }
            finally
            {
                _starting = false;
                _hasRun   = true;
            }
        }