/// <summary> /// Executes after the handling request. /// </summary> /// <param name="context"> /// The context. /// </param> public void AfterHandlingRequest(RequestProcessingContext context) { if (context != null && context.Response != null) { try { if (context.Response.Exception == null) { if (_validationFailures.Count > 0) { if (context.Response is IDtoResponse) { var dto = (context.Response as IDtoResponse).GetDto(); MapFailures(_validationFailures, dto); } else { Logger.Error( "Validation failed but no failures mapped to data transfer object, because the response type ({0}) is not derived from {1}", context.Response.GetType().Name, typeof(IDtoResponse).Name); } } } } catch (Exception e) { var response = context.Response; response.Exception = new ExceptionInfo(e); response.ExceptionType = ExceptionType.Unknown; } } }
public virtual void BeforeHandlingRequest(RequestProcessingContext context) { var testRequest = (SpyRequest)context.Request; testRequest.BeforeProcessingTimeStamp = SystemClock.Now(); Thread.Sleep(50); }
private void HandleRequest(RequestProcessingContext requestProcessingState) { var request = requestProcessingState.Request; if (request is OneWayRequest) { using (var handler = (IOneWayRequestHandler)IoC.Container.Resolve(GetOneWayRequestHandlerTypeFor(request))) { try { ExecuteHandler((OneWayRequest)request, handler); } finally { IoC.Container.Release(handler); } } } else { using (var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(request))) { try { var response = GetResponseFromHandler(request, handler); requestProcessingState.MarkAsProcessed(response); } finally { IoC.Container.Release(handler); } } } }
public void AfterHandlingRequest(RequestProcessingContext context) { var testRequest = (SpyRequest)context.Request; testRequest.AfterProcessingTimeStamp = SystemClock.Now(); testRequest.ResponseFromContext = context.Response; Thread.Sleep(50); }
public void DealWithPreviouslyOccurredExceptions(RequestProcessingContext context) { var response = CreateResponse(context); response.Exception = new ExceptionInfo(new Exception(ExceptionType.EarlierRequestAlreadyFailed.ToString())); response.ExceptionType = ExceptionType.EarlierRequestAlreadyFailed; context.MarkAsProcessed(response); }
public void AfterHandlingRequest(RequestProcessingContext context) { if (ResponseCanBeCached(context)) { context.Response.IsCached = true; cacheManager.StoreInCache(context.Request, context.Response); } }
public void DealWithException(RequestProcessingContext context, Exception exception) { var response = CreateResponse(context); response.Exception = new ExceptionInfo(exception); SetExceptionType(response, exception); context.MarkAsProcessed(response); }
private Type TryBasedOnConventions(RequestProcessingContext context) { var conventions = IoC.Container.TryResolve <IConventions>(); if (conventions != null) { return(conventions.GetResponseTypeFor(context.Request)); } return(null); }
protected override void Given() { IoC.Container = new Container(); errorHandler = new RequestProcessingErrorHandler(new ServiceLayerConfiguration(IoC.Container)); request = new RequestA(); Context = new RequestProcessingContext(request); cacheManager = Stubbed <ICacheManager>(); Container.RegisterInstance(cacheManager); EstablishContext(); }
public void BeforeHandlingRequest(RequestProcessingContext context) { if (CachingIsEnabledForThisRequest(context)) { var response = cacheManager.GetCachedResponseFor(context.Request); if (response != null) { context.MarkAsProcessed(response); } } }
private Type TryBasedOnRequestHandler(RequestProcessingContext context) { try { var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(context.Request)); return(handler.CreateDefaultResponse().GetType()); } catch { return(null); } }
/// <summary> /// Befores the handling request. /// </summary> /// <param name="context">The context.</param> public void BeforeHandlingRequest(RequestProcessingContext context) { if (context != null && context.Request != null) { var request = context.Request; var resource = new ResourceRequest { request.GetType().FullName }; if (!_accessControlManager.CanAccess(resource)) { throw new InvalidOperationException("You do not have permission to access: " + resource); } } }
public override void BeforeHandlingRequest(RequestProcessingContext context) { try { Validate.Object(context.Request as dynamic); } catch (ValidationException exc) { var response = CreateDefaultResponseFor(context.Request); response.Exception = new ExceptionInfo(exc); response.ExceptionType = ExceptionType.Unknown; context.MarkAsProcessed(response); } }
private Type TryBasedOnCachedResponse(RequestProcessingContext context) { var cacheManager = IoC.Container.Resolve <ICacheManager>(); if (cacheManager.IsCachingEnabledFor(context.Request.GetType())) { var response = cacheManager.GetCachedResponseFor(context.Request); if (response != null) { return(response.GetType()); } } return(null); }
private IEnumerable <Exception> RunInvokedInterceptorsSafely(RequestProcessingContext requestProcessingState, IList <IRequestHandlerInterceptor> invokedInterceptors) { var exceptionsFromInterceptor = new List <Exception>(); foreach (var interceptor in invokedInterceptors.Reverse()) { try { interceptor.AfterHandlingRequest(requestProcessingState); } catch (Exception exc) { exceptionsFromInterceptor.Add(exc); } } return(exceptionsFromInterceptor); }
private Type DetermineResponseType(RequestProcessingContext context) { var strategies = new Func <RequestProcessingContext, Type>[] { TryBasedOnConventions, TryBasedOnCachedResponse, TryBasedOnRequestHandler }; foreach (var strategy in strategies) { var responseType = strategy(context); if (responseType != null) { return(responseType); } } return(typeof(Response)); }
private bool InvokeRequestHandler(RequestProcessingContext requestProcessingState) { var request = requestProcessingState.Request; BeforeResolvingRequestHandler(request); using (var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(request))) { try { var response = GetResponseFromHandler(request, handler); requestProcessingState.MarkAsProcessed(response); return(response.ExceptionType != ExceptionType.None); } finally { IoC.Container.Release(handler); } } }
private Type TryBasedOnRequestHandler(RequestProcessingContext context) { IRequestHandler handler = null; try { handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(context.Request)); return(handler.CreateDefaultResponse().GetType()); } catch { return(null); } finally { if (handler != null) { IoC.Container.Release(handler); } } }
private void InvokeRequestHandler(RequestProcessingContext requestProcessingState) { BeforeResolvingRequestHandler(requestProcessingState.Request); HandleRequest(requestProcessingState); }
public abstract void AfterHandlingRequest(RequestProcessingContext context);
public abstract void BeforeHandlingRequest(RequestProcessingContext context);
private bool ResponseCanBeCached(RequestProcessingContext context) { return CachingIsEnabledForThisRequest(context) && context.Response.ExceptionType == ExceptionType.None; }
/// <summary> /// Executes before the handling request. /// </summary> /// <param name="context"> /// The context. /// </param> public void BeforeHandlingRequest(RequestProcessingContext context) { DomainEvent.Register <RuleViolationEvent> (failure => _validationFailures.AddRange(failure.RuleViolations)); }
public override void AfterHandlingRequest(RequestProcessingContext context) { base.AfterHandlingRequest(context); throw new InvalidOperationException("I will fail."); }
private void DispatchRequestsToHandlers(List <Response> responses, params Request[] requests) { bool exceptionsPreviouslyOccurred = false; foreach (var request in requests) { var requestProcessingState = new RequestProcessingContext(request); IList <IRequestHandlerInterceptor> interceptors = new List <IRequestHandlerInterceptor>(); try { IList <IRequestHandlerInterceptor> invokedInterceptors = new List <IRequestHandlerInterceptor>(); if (!exceptionsPreviouslyOccurred) { interceptors = ResolveInterceptors(); foreach (var interceptor in interceptors) { interceptor.BeforeHandlingRequest(requestProcessingState); invokedInterceptors.Add(interceptor); if (requestProcessingState.IsProcessed) { responses.Add(requestProcessingState.Response); break; } } } if (!requestProcessingState.IsProcessed) { var skipHandler = false; var cachingIsEnabledForThisRequest = _cacheManager.IsCachingEnabledFor(request.GetType()); if (cachingIsEnabledForThisRequest) { var cachedResponse = _cacheManager.GetCachedResponseFor(request); if (cachedResponse != null) { if (exceptionsPreviouslyOccurred) { var dummyResponse = Activator.CreateInstance(cachedResponse.GetType()) as Response; responses.Add(SetStandardExceptionInfoWhenEarlierRequestsFailed(dummyResponse)); requestProcessingState.MarkAsProcessed(dummyResponse); } else { responses.Add(cachedResponse); requestProcessingState.MarkAsProcessed(cachedResponse); } skipHandler = true; } } if (!skipHandler) { BeforeResolvingRequestHandler(request); using (var handler = (IRequestHandler)IoC.Container.Resolve(GetRequestHandlerTypeFor(request))) { try { if (!exceptionsPreviouslyOccurred) { var response = GetResponseFromHandler(request, handler); exceptionsPreviouslyOccurred = response.ExceptionType != ExceptionType.None; responses.Add(response); requestProcessingState.MarkAsProcessed(response); if (response.ExceptionType == ExceptionType.None && cachingIsEnabledForThisRequest) { _cacheManager.StoreInCache(request, response); } } else { var response = handler.CreateDefaultResponse(); responses.Add(SetStandardExceptionInfoWhenEarlierRequestsFailed(response)); requestProcessingState.MarkAsProcessed(response); } } finally { IoC.Container.Release(handler); } } } } foreach (var interceptor in invokedInterceptors.Reverse()) { interceptor.AfterHandlingRequest(requestProcessingState); } invokedInterceptors.Clear(); } finally { SaveDisposeInterceptors(interceptors); } } }
private bool CachingIsEnabledForThisRequest(RequestProcessingContext context) { return cacheManager.IsCachingEnabledFor(context.Request.GetType()); }
public virtual void AfterHandlingRequest(RequestProcessingContext context) { ((SpyRequest)context.Request).SubSequentInterceptorAfterProcessingTimeStamp = SystemClock.Now(); Thread.Sleep(50); }
private bool ResponseCanBeCached(RequestProcessingContext context) { return(CachingIsEnabledForThisRequest(context) && context.Response.ExceptionType == ExceptionType.None); }
public void BeforeHandlingRequest(RequestProcessingContext context) { ((SpyRequest)context.Request).SubSequentInterceptorBeforeProcessingTimeStamp = SystemClock.Now(); Thread.Sleep(50); }
private Response CreateResponse(RequestProcessingContext context) { var responseType = DetermineResponseType(context); return((Response)Activator.CreateInstance(responseType)); }
private bool CachingIsEnabledForThisRequest(RequestProcessingContext context) { return(cacheManager.IsCachingEnabledFor(context.Request.GetType())); }
public override void BeforeHandlingRequest(RequestProcessingContext context) { context.MarkAsProcessed(new ResponseFromInterceptor()); }