示例#1
0
        internal void ProcessRequestInternal(HttpContextBase context)
        {
            // Read post data or request parameters
            var data = ReadPostData(context);

            if (string.IsNullOrEmpty(data))
            {
                data = context.Request["id"];
            }

            // Get the processor for the request and check authorization.
            var method = context.Request["m"];
            IRequestProcessor requestProcessor;

            try
            {
                requestProcessor = _requestProcessorFactory.Create(method);
            }
            catch (InvalidOperationException ex)
            {
                // Handle requests for invalid request types.
                if (!ex.Message.Contains("Dependency creator not registered"))
                {
                    throw;
                }
                requestProcessor = _requestProcessorFactory.Create(RequestTypes.NotFound);
            }

            if (!IsAuthorized(requestProcessor))
            {
                // If authorization fails, replace the processor with the unauthorized processor.
                requestProcessor = _requestProcessorFactory.Create(RequestTypes.Unauthorized);
            }

            // Process the request
            var response = requestProcessor.Process(data);

            // Create not found response when processor can not find it's data.
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                requestProcessor = _requestProcessorFactory.Create(RequestTypes.NotFound);
                response         = requestProcessor.Process(null);
            }

            // Return the response to the user.
            WriteResponse(context, response);
        }
示例#2
0
        private async Task ReaderThread(CancellationToken ct)
        {
            while (true)
            {
                ct.ThrowIfCancellationRequested();

                try
                {
                    await _readerSemaphore.WaitAsync(ct);

                    ct.ThrowIfCancellationRequested();

                    _readerStream.Position = 0;
                    var count = await _pipe.CopyMessageToAsync(_readerStream, ct);

                    if (count == 0)
                    {
                        _readerSemaphore.Release();
                        Stop();
                        await StartAsync().ConfigureAwait(false);

                        return;
                    }

                    _readerStream.Position = 0;
                    var request = await _messageSerializer.DeserializeAsync <ApiRequest>(_readerStream).ConfigureAwait(false);

                    if (request == null)
                    {
                        continue;
                    }

                    var requestProcessor = _requestProcessorFactory.Create(request.Resource);
                    var response         = await requestProcessor.ProcessAsync(request.Action, request.Content).ConfigureAwait(false);

                    await Task.Run(() => _pipe.WaitForPipeDrain()).ConfigureAwait(false);

                    await _messageSerializer.SerializeAsync(_pipe, response).ConfigureAwait(false);

                    _readerSemaphore.Release();
                }
                catch (OperationCanceledException)
                {
                    _readerSemaphore.Release();
                    return;
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    _readerSemaphore.Release();
                    Stop();
                    await StartAsync().ConfigureAwait(false);
                }
            }
        }