public virtual object Execute(IRequest request, object instance, TRequest requestDto) { try { BeforeEachRequest(request, requestDto); var container = HostContext.Container; if (RequestFilters != null) { foreach (var requestFilter in RequestFilters) { var attrInstance = requestFilter.Copy(); container.AutoWire(attrInstance); attrInstance.RequestFilter(request, request.Response, requestDto); AppHost.Release(attrInstance); if (request.Response.IsClosed) { return(null); } } } var response = AfterEachRequest(request, requestDto, ServiceAction(instance, requestDto)); if (ResponseFilters != null) { foreach (var responseFilter in ResponseFilters) { var attrInstance = responseFilter.Copy(); container.AutoWire(attrInstance); attrInstance.ResponseFilter(request, request.Response, response); AppHost.Release(attrInstance); if (request.Response.IsClosed) { return(null); } } } return(response); } catch (Exception ex) { var result = HandleException(request, requestDto, ex); if (result == null) { throw; } return(result); } }
public virtual async Task <object> ExecuteAsync(IRequest req, object instance, TRequest requestDto) { try { BeforeEachRequest(req, requestDto, instance); var res = req.Response; var container = HostContext.Container; if (RequestFilters != null) { foreach (var requestFilter in RequestFilters) { var attrInstance = requestFilter.Copy(); container.AutoWire(attrInstance); if (attrInstance is IHasRequestFilter filterSync) { filterSync.RequestFilter(req, res, requestDto); } else if (attrInstance is IHasRequestFilterAsync filterAsync) { await filterAsync.RequestFilterAsync(req, res, requestDto); } AppHost.Release(attrInstance); if (res.IsClosed) { return(null); } } } var response = AfterEachRequest(req, requestDto, ServiceAction(instance, requestDto), instance); if (HostContext.StrictMode) { if (response != null && response.GetType().IsValueType) { throw new StrictModeException( $"'{requestDto.GetType().Name}' Service cannot return Value Types for its Service Responses. " + $"You can embed its '{response.GetType().Name}' return value in a Response DTO or return as raw data in a string or byte[]", StrictModeCodes.ReturnsValueType); } } if (response is Task taskResponse) { if (taskResponse.Status == TaskStatus.Created) { taskResponse.Start(); } await taskResponse; response = taskResponse.GetResult(); } LogRequest(req, requestDto, response); if (response is IHttpError error) { var ex = (Exception)error; var result = await HandleExceptionAsync(req, requestDto, ex, instance); if (result == null) { throw ex; } return(result); } if (ResponseFilters != null) { foreach (var responseFilter in ResponseFilters) { var attrInstance = responseFilter.Copy(); container.AutoWire(attrInstance); if (attrInstance is IHasResponseFilter filter) { filter.ResponseFilter(req, res, response); } else if (attrInstance is IHasResponseFilterAsync filterAsync) { await filterAsync.ResponseFilterAsync(req, res, response); } AppHost.Release(attrInstance); if (res.IsClosed) { return(null); } } } return(response); } catch (Exception ex) { //Sync Exception Handling var result = await HandleExceptionAsync(req, requestDto, ex, instance); if (result == null) { throw; } return(result); } }
public virtual object Execute(IRequest req, object instance, TRequest requestDto) { try { BeforeEachRequest(req, requestDto); var container = HostContext.Container; if (RequestFilters != null) { foreach (var requestFilter in RequestFilters) { var attrInstance = requestFilter.Copy(); container.AutoWire(attrInstance); attrInstance.RequestFilter(req, req.Response, requestDto); AppHost.Release(attrInstance); if (req.Response.IsClosed) { return(null); } } } var response = AfterEachRequest(req, requestDto, ServiceAction(instance, requestDto)); if (HostContext.StrictMode) { if (response != null && response.GetType().IsValueType()) { throw new StrictModeException($"'{requestDto.GetType().Name}' Service cannot return Value Types for its Service Responses. " + $"You can embed its '{response.GetType().Name}' return value in a Response DTO or return as raw data in a string or byte[]", StrictModeCodes.ReturnsValueType); } } var taskResponse = response as Task; if (taskResponse != null) { if (taskResponse.Status == TaskStatus.Created) { taskResponse.Start(); } return(HostContext.Async.ContinueWith(req, taskResponse, task => { if (task.IsFaulted) { var ex = task.Exception.UnwrapIfSingleException(); //Async Exception Handling var result = HandleException(req, requestDto, ex); if (result == null) { return ex; } return result; } response = task.GetResult(); LogRequest(req, response); if (ResponseFilters != null) { //Async Exec ResponseFilters foreach (var responseFilter in ResponseFilters) { var attrInstance = responseFilter.Copy(); container.AutoWire(attrInstance); attrInstance.ResponseFilter(req, req.Response, response); AppHost.Release(attrInstance); if (req.Response.IsClosed) { return null; } } } return response; })); } else { LogRequest(req, response); } var error = response as IHttpError; if (error != null) { var ex = (Exception)error; var result = HandleException(req, requestDto, ex); if (result == null) { throw ex; } return(result); } //Sync Exec ResponseFilters if (ResponseFilters != null) { foreach (var responseFilter in ResponseFilters) { var attrInstance = responseFilter.Copy(); container.AutoWire(attrInstance); attrInstance.ResponseFilter(req, req.Response, response); AppHost.Release(attrInstance); if (req.Response.IsClosed) { return(null); } } } return(response); } catch (Exception ex) { //Sync Exception Handling var result = HandleException(req, requestDto, ex); if (result == null) { throw; } return(result); } }
public virtual object Execute(IRequest request, object instance, TRequest requestDto) { try { BeforeEachRequest(request, requestDto); var container = HostContext.Container; if (RequestFilters != null) { foreach (var requestFilter in RequestFilters) { var attrInstance = requestFilter.Copy(); container.AutoWire(attrInstance); attrInstance.RequestFilter(request, request.Response, requestDto); AppHost.Release(attrInstance); if (request.Response.IsClosed) { return(null); } } } var response = AfterEachRequest(request, requestDto, ServiceAction(instance, requestDto)); var taskResponse = response as Task; if (taskResponse != null) { if (taskResponse.Status == TaskStatus.Created) { taskResponse.Start(); } return(taskResponse.ContinueWith(task => { if (task.IsFaulted) { var ex = task.Exception.UnwrapIfSingleException(); //Async Exception Handling var result = HandleException(request, requestDto, ex); if (result == null) { return ex; } return result; } response = task.GetResult(); if (ResponseFilters != null) { //Async Exec ResponseFilters foreach (var responseFilter in ResponseFilters) { var attrInstance = responseFilter.Copy(); container.AutoWire(attrInstance); attrInstance.ResponseFilter(request, request.Response, response); AppHost.Release(attrInstance); if (request.Response.IsClosed) { return null; } } } return response; })); } //Sync Exec ResponseFilters if (ResponseFilters != null) { foreach (var responseFilter in ResponseFilters) { var attrInstance = responseFilter.Copy(); container.AutoWire(attrInstance); attrInstance.ResponseFilter(request, request.Response, response); AppHost.Release(attrInstance); if (request.Response.IsClosed) { return(null); } } } return(response); } catch (Exception ex) { //Sync Exception Handling var result = HandleException(request, requestDto, ex); if (result == null) { throw; } return(result); } }