Esempio n. 1
0
        public async Task <Response <TResponseData> > RequestAsync <TResponseData>(IAsyncRequest <TResponseData> query)
        {
            var response = new Response <TResponseData>();

            try
            {
                var plan = new MediatorPlan <TResponseData>(typeof(IAsyncRequestHandler <,>), "HandleAsync", query.GetType(), _dependencyResolver);

                var interceptors = GetRequestInterceptors(plan);

                foreach (var interceptor in interceptors)
                {
                    await interceptor.BeforeInvokeAsync(plan.HandleMethod, query, query.GetType());
                }

                response.Data = await plan.InvokeAsync(query);

                foreach (var interceptor in interceptors)
                {
                    await interceptor.AfterInvokeAsync(plan.HandleMethod, query, query.GetType(), response.Data);
                }
            }
            catch (Exception e)
            {
                response.Exception = e;
            }

            return(response);
        }
Esempio n. 2
0
        public async Task <IAsyncResponse> ExecuteAsync(IAsyncRequest request)
        {
            try
            {
                _log.DebugFormat("Recieved async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

                var router = IoC.Container.Resolve <IAsyncRouter>();

                var response = await router.ExecuteRequestAsync(request);

                _log.DebugFormat("Returning async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

                return(response);
            }
            catch (Exception ex)
            {
                _log.DebugFormat("Failed async request : {0}, Id - {1} @ {2}. {3}", request.GetType().Name, request.Id, DateTime.Now, ex.Message);

                var genericFault = new GenericFault
                {
                    Operation = request.GetType().FullName,
                    Message   = ex.Message
                };

                throw new FaultException <GenericFault>(genericFault, ex.Message);
            }
        }
Esempio n. 3
0
        public async Task<IAsyncResponse> ExecuteAsync(IAsyncRequest request)
        {
            try
            {
                _log.DebugFormat("Recieved async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

                var router = IoC.Container.Resolve<IAsyncRouter>();

                var response = await router.ExecuteRequestAsync(request);

                _log.DebugFormat("Returning async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

                return response;
            }
            catch (Exception ex)
            {
                _log.DebugFormat("Failed async request : {0}, Id - {1} @ {2}. {3}", request.GetType().Name, request.Id, DateTime.Now, ex.Message);

                var genericFault = new GenericFault
                {
                    Operation = request.GetType().FullName,
                    Message = ex.Message
                };

                throw new FaultException<GenericFault>(genericFault, ex.Message);
            }
        }
Esempio n. 4
0
        private AsyncRequestHandler <TResponse> GetHandler <TResponse>(IAsyncRequest <TResponse> request)
        {
            var handlerType = typeof(IAsyncRequestHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse));
            var wrapperType = typeof(AsyncRequestHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse));
            var handler     = _container.HasComponent(handlerType) ? _container.Resolve(handlerType) : null;

            if (handler == null)
            {
                throw BuildException(request);
            }

            var wrapperHandler = Activator.CreateInstance(wrapperType, handler);

            return((AsyncRequestHandler <TResponse>)wrapperHandler);
        }
Esempio n. 5
0
        private AsyncRequestHandler <TResponse> GetHandler <TResponse>(IAsyncRequest <TResponse> request)
        {
            var handlerType = typeof(IAsyncRequestHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse));
            var wrapperType = typeof(AsyncRequestHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse));
            var handler     = _serviceLocatorProvider().GetInstance(handlerType);

            if (handler == null)
            {
                throw BuildException(request);
            }

            var wrapperHandler = Activator.CreateInstance(wrapperType, handler);

            return((AsyncRequestHandler <TResponse>)wrapperHandler);
        }
Esempio n. 6
0
        public async Task<IAsyncResponse> ExecuteRequestAsync(IAsyncRequest request)
        {
            _log.DebugFormat("Started executing async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

            var requestType = request.GetType();
            var responseType = _requestResponseService.GetMatchingAsyncResponseType(requestType);

            var requestHandlerType = typeof (IAsyncRequestHandler<,>).MakeGenericType(requestType, responseType);

            var requestHandler = (IAsyncRequestHandler) IoC.Container.Resolve(requestHandlerType);

            var response = await requestHandler.ExecuteAsync(request);

            _log.DebugFormat("Finished executing async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

            return response;
        }
Esempio n. 7
0
        public async Task <TResult> Handle <TResult>(IAsyncRequest <TResult> request)
        {
            var genericRequestHandler = typeof(IAsyncRequestHandler <,>);
            var handlerType           = genericRequestHandler.MakeGenericType(request.GetType(), typeof(TResult));

            var handler = _handlerFactory(handlerType);

            return(await(Task <TResult>) handler.GetType().GetMethod("Handle").Invoke(handler, new object[] { request }));
        }
Esempio n. 8
0
        public async Task <TResponse> GetAsync <TResponse>(IAsyncRequest <TResponse> request)
            where TResponse : IAsyncResponse
        {
            using (var performanceTester = new PerformanceTester())
            {
                using (var routerServiceProxy = _asyncRouterServiceProxyFactory())
                {
                    try
                    {
                        request.Id = Guid.NewGuid();

                        _log.Debug(string.Format("Start RequestTask {0}, Id - {1}", request.GetType(), request.Id));

                        var response = await routerServiceProxy.ExecuteAsync(request);

                        _log.Debug(string.Format("Finished RequestTask {0}, Id - {1}. Duration {2}",
                                                 request.GetType(),
                                                 request.Id,
                                                 performanceTester.Result.Milliseconds));

                        return((TResponse)response);
                    }
                    catch (FaultException <GenericFault> faultException)
                    {
                        throw new RequestException(string.Format("RequestTask failed for {0}, Id - {1}. Reason '{2}'",
                                                                 request.GetType(),
                                                                 request.Id,
                                                                 faultException.Detail.Message),
                                                   faultException);
                    }
                    catch (Exception ex)
                    {
                        throw new RequestException(string.Format("RequestTask failed for {0}, Id - {1}", request.GetType(), request.Id), ex);
                    }
                }
            }
        }
Esempio n. 9
0
        private AsyncRequestHandler <TResponse> GetHandler <TResponse>(IAsyncRequest <TResponse> request)
        {
            var    handlerType = typeof(IAsyncRequestHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse));
            var    wrapperType = typeof(AsyncRequestHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse));
            object handler;

            try
            {
                handler = _singleInstanceFactory(handlerType);

                if (handler == null)
                {
                    throw BuildException(request);
                }
            }
            catch (Exception e)
            {
                throw BuildException(request, e);
            }

            var wrapperHandler = Activator.CreateInstance(wrapperType, handler);

            return((AsyncRequestHandler <TResponse>)wrapperHandler);
        }
Esempio n. 10
0
        public async Task <IAsyncResponse> ExecuteRequestAsync(IAsyncRequest request)
        {
            _log.DebugFormat("Started executing async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

            var requestType  = request.GetType();
            var responseType = _requestResponseService.GetMatchingAsyncResponseType(requestType);

            var requestHandlerType = typeof(IAsyncRequestHandler <,>).MakeGenericType(requestType, responseType);

            var requestHandler = (IAsyncRequestHandler)IoC.Container.Resolve(requestHandlerType);

            var response = await requestHandler.ExecuteAsync(request);

            _log.DebugFormat("Finished executing async request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

            return(response);
        }
Esempio n. 11
0
        public async Task <Response <TResponse> > RequestAsync <TResponse>(IAsyncRequest <TResponse> query)
        {
            var response = new Response <TResponse>();

            try {
                var plan = new MediatorPlan <TResponse>(typeof(IAsyncRequestHandler <,>), "HandleAsync", query.GetType(), _dependencyResolver);

                response.Data = await plan.InvokeAsync(query);
            }
            catch (Exception e) {
                response.Exception = e;
            }

            return(response);
        }
Esempio n. 12
0
        public async Task <IAsyncResponse> ExecuteAsync(IAsyncRequest request)
        {
            var typedRequest = (TRequest)request;

            _log.DebugFormat("Executing BeforeExecuteAsync on sync request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

            typedRequest = await BeforeExecuteAsync(typedRequest);

            _log.DebugFormat("Executing HandleAsync on sync request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

            var response = await HandleAsync(typedRequest);

            _log.DebugFormat("Executing AfterExecuteAsync on sync request : {0}, Id - {1} @ {2}", request.GetType().Name, request.Id, DateTime.Now);

            response = await AfterExecuteAsync(response);

            return(response);
        }
Esempio n. 13
0
        public Task <TResponseData> RequestAsync <TResponseData>(IAsyncRequest <TResponseData> query)
        {
            var plan = new MediatorPlan <TResponseData>(typeof(IAsyncRequestHandler <,>), "HandleAsync", query.GetType(), _container);

            return(plan.InvokeAsync(query));
        }