예제 #1
0
        protected override Task <HttpResponseMessage> ProcessRequest(HttpRequestMessage requestMessage, RequestRouterConfiguration configuration,
                                                                     CancellationToken cancellationToken)
        {
            RequestRouterConfigurationBase realConfiguration = string.IsNullOrWhiteSpace(configuration.Service) ? (RequestRouterConfigurationBase)((HostRouterConfiguration)configuration) : (RequestRouterConfigurationBase)((ConsulServiceRouterConfiguration)configuration);
            var handler = _requestHandlerFactory.GetHandler();

            try
            {
                return(handler.HandleRequest(requestMessage, configuration, cancellationToken));
            }
            finally
            {
                _requestHandlerFactory.Release(handler);
            }
        }
        public async Task <TResponse> ExecuteAsync <TRequest, TResponse>(TRequest request)
            where TRequest : class where TResponse : class
        {
            var requestType = typeof(TRequest).FullName?.Replace("Ucb.Btp.Contracts.Requests.", "");
            var stopwatch   = new Stopwatch();
            IRequestHandler <TRequest, TResponse> handler = null;

            using var scope = _container.CreateScope();
            try
            {
                stopwatch.Start();

                var validationErrors = new List <ValidationResult>();
                Validator.TryValidateObject(request, new ValidationContext(request), validationErrors, true);
                //if (validationErrors.Any())
                //    throw new Domain.Exceptions.ValidationException(
                //        Domain.Exceptions.ValidationException.ValidationErrors.Generic, "ContractValidation",
                //        validationErrors);

                //handler = _handlerFactory.Resolve<TRequest, TResponse>();
                handler = (IRequestHandler <TRequest, TResponse>)scope.ServiceProvider.GetService(
                    typeof(IRequestHandler <TRequest, TResponse>));
                var returnValue = await handler.HandleAsync(request);

                stopwatch.Stop();
                _logger.LogInformation(InfoMessage, requestType, stopwatch.ElapsedMilliseconds);
                return(returnValue);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ErrorMessage, requestType, stopwatch.ElapsedMilliseconds);
                throw;
            }
            finally
            {
                if (handler != null)
                {
                    _handlerFactory.Release(handler);
                }
            }
        }