public static void EndHttpHandlerRequest(IResponse httpRes, bool skipHeaders = false, bool skipClose = false, Action <IResponse> afterHeaders = null) { //if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders(); afterHeaders?.Invoke(httpRes); var req = httpRes.Request; //if (req != null && !req.Items.ContainsKey(Keywords.HasLogged)) { HostContext.TryResolve <IRequestLogger>()?.Log(req, req.Dto, httpRes.Dto, TimeSpan.Zero); } if (!skipClose && !httpRes.IsClosed) { httpRes.Close(); } HostContext.CompleteRequest(req); }
public static async Task EndHttpHandlerRequestAsync(IResponse httpRes, bool skipHeaders = false, bool skipClose = false, Func <IResponse, Task> afterHeaders = null) { //if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders(); if (afterHeaders != null) { await afterHeaders(httpRes); } var req = httpRes.Request; //if (req != null && !req.Items.ContainsKey(Keywords.HasLogged)) //{ // HostContext.TryResolve<IRequestLogger>()?.Log(req, req.Dto, httpRes.Dto, TimeSpan.Zero); //} if (!skipClose && !httpRes.IsClosed) { await httpRes.CloseAsync(); } HostContext.CompleteRequest(req); }
protected Message ExecuteMessage(Message message, RequestAttributes requestAttributes, IRequest httpReq, IResponse httpRes) { var soapFeature = requestAttributes.ToSoapFeature(); appHost.AssertFeatures(soapFeature); if (httpReq == null) { httpReq = HostContext.GetCurrentRequest(); } if (httpRes == null && httpReq != null) { httpRes = httpReq.Response; } if (httpReq == null) { throw new ArgumentNullException("httpReq"); } if (httpRes == null) { throw new ArgumentNullException("httpRes"); } httpReq.UseBufferedStream = true; var requestMsg = message ?? GetRequestMessageFromStream(httpReq.InputStream); var soapAction = httpReq.GetHeader(HttpHeaders.SOAPAction) ?? GetAction(requestMsg); if (soapAction != null) { httpReq.OperationName = soapAction.Trim('"'); } if (HostContext.ApplyCustomHandlerRequestFilters(httpReq, httpRes)) { return(PrepareEmptyResponse(message, httpReq)); } string requestXml = GetRequestXml(requestMsg); var requestType = GetRequestType(requestMsg, requestXml); httpReq.OperationName = requestType.GetOperationName(); if (!HostContext.Metadata.CanAccess(requestAttributes, soapFeature.ToFormat(), requestType.GetOperationName())) { throw HostContext.UnauthorizedAccess(requestAttributes); } try { var useXmlSerializerRequest = requestType.HasAttribute <XmlSerializerFormatAttribute>(); var request = appHost.ApplyRequestConverters(httpReq, useXmlSerializerRequest ? XmlSerializableSerializer.Instance.DeserializeFromString(requestXml, requestType) : Serialization.DataContractSerializer.Instance.DeserializeFromString(requestXml, requestType) ); httpReq.Dto = request; var requiresSoapMessage = request as IRequiresSoapMessage; if (requiresSoapMessage != null) { requiresSoapMessage.Message = requestMsg; } httpReq.SetItem(Keywords.SoapMessage, requestMsg); httpRes.ContentType = GetSoapContentType(httpReq.ContentType); var hasRequestFilters = HostContext.GlobalRequestFilters.Count > 0 || FilterAttributeCache.GetRequestFilterAttributes(request.GetType()).Any(); if (hasRequestFilters && HostContext.ApplyRequestFilters(httpReq, httpRes, request)) { return(EmptyResponse(requestMsg, requestType)); } httpReq.RequestAttributes |= requestAttributes; var response = ExecuteService(request, httpReq); var taskResponse = response as Task; if (taskResponse != null) { taskResponse.Wait(); response = TypeAccessor.Create(taskResponse.GetType())[taskResponse, "Result"]; } response = HostContext.AppHost.ApplyResponseConverters(httpReq, response); var hasResponseFilters = HostContext.GlobalResponseFilters.Count > 0 || FilterAttributeCache.GetResponseFilterAttributes(response.GetType()).Any(); if (hasResponseFilters && HostContext.ApplyResponseFilters(httpReq, httpRes, response)) { return(EmptyResponse(requestMsg, requestType)); } var httpResult = response as IHttpResult; if (httpResult != null) { response = httpResult.Response; } var noMsgAction = requestMsg.Headers.Action == null; var responseMsg = CreateResponseMessage(response, requestMsg.Version, requestType, noMsgAction); if (httpResult != null) { SetErrorStatusIfAny(httpReq.Response, responseMsg, httpResult.Status); } return(responseMsg); } catch (Exception ex) { if (httpReq.Dto != null) { HostContext.RaiseServiceException(httpReq, httpReq.Dto, ex); } else { HostContext.RaiseUncaughtException(httpReq, httpRes, httpReq.OperationName, ex); } throw new SerializationException("3) Error trying to deserialize requestType: " + requestType + ", xml body: " + requestXml, ex); } finally { HostContext.CompleteRequest(httpReq); } }