/// <summary>
        /// The is the core function to create a response. This function will not be called
        /// if <see cref="IRequestHandler.IsMatch"/> returns <c>false</c>.
        /// </summary>
        /// <param name="request">The request message received.</param>
        /// <param name="parameters">The parameters that are extracted using the <see cref="IRequestHandler.IsMatch"/>
        /// method.</param>
        /// <param name="cancellationToken">
        /// The cancellation token passed to the async method.
        /// </param>
        /// <param name="logger">
        /// The verbose logger.
        /// </param>
        /// <returns>The generated http response message.</returns>
        public async Task <HttpResponseMessage> HandleAsync(
            HttpRequestMessage request,
            IDictionary <string, object> parameters,
            CancellationToken cancellationToken,
            IServerLogger logger = null)
        {
            IServerLogger actualLogger = logger ?? new DummyLogger();

            if (Name != null)
            {
                HttpRequestMessage cloned = await CloneHttpRequestMessageAsync(request).ConfigureAwait(false);

                actualLogger.Log($"[Handler] Record calling history with name '{Name}'");
                callingHistories.Enqueue(new CallingHistoryContext(cloned, parameters));
            }

            try
            {
                return(CreateResponse(request, parameters, cancellationToken));
            }
            catch (Exception error)
            {
                actualLogger.Log($"[Handler] Unexpected error occured during handler evaluation: {error.Message}. Will return Internal Server Error");
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(error.ToString())
                });
            }
        }
        public async Task StartLoop()
        {
            _logger.Log("Starting the loop");
            _payloads = await _payloadLoader.GetPayloadsAsync();

            _poll.SetPayloadSource(_payloads);

            while (true)
            {
                await Task.Delay(TimeSpan.FromSeconds(1));
                await UpdatePollAsync();
            }
        }
Exemple #3
0
        private static void RepeatIfFailed(Action action, IServerLogger serverLogger, int attempt = 0)
        {
            try
            {
                action();
            }
            catch (Exception e)
            {
                var delay = AttemptDelay * Math.Pow(2, attempt);

                serverLogger.Log($"Internet is{(Internet.IsAvailable() ? null : " NOT")} available");
                serverLogger.Log(e.GetFullDescription($"Attempt #{attempt} failed, next try in {delay}"));

                Thread.Sleep(delay);
                RepeatIfFailed(action, serverLogger, attempt + 1);
            }
        }
Exemple #4
0
 private static void UnhandledExceptionTrapper(UnhandledExceptionEventArgs e, IServerLogger serverLogger)
 {
     serverLogger.Log(e.ExceptionObject.ToString());
 }