コード例 #1
0
        /// <summary>
        /// Handle process result
        /// </summary>
        /// <typeparam name="TProcess"></typeparam>
        /// <param name="process"></param>
        /// <param name="type"></param>
        /// <param name="response"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public MiddlewareResult HandleAfterExecution <TProcess>(TProcess process, Type type, VoidResult response, IHttpContextWrapper context) where TProcess : IBaseProcess <VoidResult>
        {
            var attributes = type.GetCustomAttributes(typeof(SignalsApiAttribute), false)
                             .Cast <SignalsApiAttribute>()
                             .ToList();

            if (!attributes.Any())
            {
                return(MiddlewareResult.DoNothing);
            }

            var correctMethod = false;

            foreach (var attribute in attributes)
            {
                correctMethod |= attribute.NativeResponse;
            }

            if (correctMethod)
            {
                var statusCode = response.IsSystemFault ? System.Net.HttpStatusCode.InternalServerError :
                                 response.IsFaulted ? System.Net.HttpStatusCode.BadRequest : System.Net.HttpStatusCode.OK;

                if (!response.IsFaulted)
                {
                    var responseType       = response.GetType();
                    var responseResultType = responseType.GetGenericArguments().FirstOrDefault();
                    if (responseResultType == null)
                    {
                        return(MiddlewareResult.DoNothing);
                    }

                    if (responseType.GetGenericTypeDefinition() == typeof(MethodResult <>))
                    {
                        var resultPropName = nameof(MethodResult <string> .Result);
                        var resultValue    = responseType.GetProperty(resultPropName)?.GetValue(response, null);
                        if (resultValue != null)
                        {
                            var resultValueSerialized = responseResultType.IsSimpleType()
                                ? resultValue.ToString()
                                : resultValue.SerializeJson();

                            context.PutResponse(new HttpResponseMessage(statusCode)
                            {
                                Content = new StringContent(resultValueSerialized, Encoding.UTF8)
                            });
                            return(MiddlewareResult.StopExecutionAndStopMiddlewarePipe);
                        }
                    }
                }
            }

            // result is not handled, continue
            return(MiddlewareResult.DoNothing);
        }