private AopController GetControllerProperties(Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor actionDescriptor, IDictionary <string, object> actionArguments)
 {
     return(new AopController {
         ControllerName = actionDescriptor.ControllerName,
         ControllerMethod = actionDescriptor.MethodInfo.Name,
         ActionArguments = UseAopObjectTrace ? PrettyPrint(actionArguments) : "AOP Arguments not enabled!"
     });
 }
Exemple #2
0
        static docMember GetMethodDocComment(DocCommentLookup lookup, Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor descriptor)
        {
            var methodInfo     = descriptor.MethodInfo;
            var methodFullName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;

            if (descriptor.Parameters.Count > 0)
            {
                methodFullName += "(" + descriptor.Parameters.Select(d => d.ParameterType.FullName).Aggregate((c, n) => c + "," + n) + ")";
            }

            return(lookup.GetMember("M:" + methodFullName));
        }
Exemple #3
0
        /// <summary>
        /// 发生异常时执行
        /// </summary>
        /// <param name="context">异常上下文</param>
        public void OnException(ExceptionContext context)
        {
            Guid requestId = Guid.NewGuid();

            if (context.HttpContext.Request.Headers.ContainsKey(Constants.REQUEST_ID_HEADER_KEY))
            {
                requestId = Guid.Parse(context.HttpContext.Request.Headers[Constants.REQUEST_ID_HEADER_KEY]);
            }
            object result = null;

            if (context.ActionDescriptor is Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)
            {
                Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor controllerActionDescriptor = context.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;

                if (controllerActionDescriptor?.MethodInfo.ReturnType == typeof(bool))
                {
                    result = default(bool);
                }
                if (controllerActionDescriptor?.MethodInfo.ReturnType == typeof(int))
                {
                    result = int.MinValue;
                }
                if (controllerActionDescriptor?.MethodInfo.ReturnType == typeof(long))
                {
                    result = long.MinValue;
                }
                if (controllerActionDescriptor?.MethodInfo.ReturnType == typeof(decimal))
                {
                    result = decimal.MinValue;
                }
            }
            if (requestId == Guid.Empty)
            {
                requestId = Guid.NewGuid();
            }
            if (context.Exception is StatusCodeException)
            {
                context.Result = BuidResult(context.Exception as StatusCodeException, requestId, result);
            }
            else
            {
                if (hostingEnvironment.IsDevelopment())
                {
                    return;
                }
                logger.LogError(context.Exception.Message);
                context.Result = BuidResult(new ErrorException(context.Exception), requestId, result);
            }
            //异常已处理
            context.ExceptionHandled = true;
        }
Exemple #4
0
        /// <summary>
        /// Translate ApiDescription of the Framework to my own WebApiDescription
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        public static WebApiDescription GetWebApiDescription(ApiDescription description)
        {
            var controllerActionDescriptor = description.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;

            if (controllerActionDescriptor == null)
            {
                return(null);
            }

            try
            {
                Type responseType;
                if (description.SupportedResponseTypes.Count > 0)
                {
                    if (description.SupportedResponseTypes[0].Type.Equals(typeof(void)))
                    {
                        responseType = null;
                    }
                    else
                    {
                        responseType = description.SupportedResponseTypes[0].Type;
                    }
                }
                else
                {
                    Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor actionDescriptor = description.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;
                    Debug.Assert(actionDescriptor != null);
                    responseType = actionDescriptor.MethodInfo.ReturnType;// in .net core 2.1, IActionResult is not in SupportedResponseTypes anymore, so I have to get it here.
                    if (responseType.Equals(typeof(void)))
                    {
                        responseType = null;
                    }
                }

                var xmlFilePath    = DocComment.DocCommentLookup.GetXmlPath(controllerActionDescriptor.MethodInfo.DeclaringType.Assembly);
                var docLookup      = DocCommentLookup.Create(xmlFilePath);
                var methodComments = docLookup == null ? null : GetMethodDocComment(docLookup, controllerActionDescriptor);

                var dr = new WebApiDescription(description.ActionDescriptor.Id)
                {
                    ActionDescriptor = new ActionDescriptor()
                    {
                        ActionName           = controllerActionDescriptor.ActionName,
                        ReturnType           = responseType,
                        ControllerDescriptor = new ControllerDescriptor()
                        {
                            ControllerName = controllerActionDescriptor.ControllerName,
                            ControllerType = controllerActionDescriptor.ControllerTypeInfo.AsType()
                        }
                    },

                    HttpMethod          = description.HttpMethod,
                    Documentation       = DocCommentHelper.GetSummary(methodComments),
                    RelativePath        = description.RelativePath + BuildQuery(description.ParameterDescriptions),
                    ResponseDescription = new ResponseDescription()
                    {
                        Documentation = DocCommentHelper.GetReturnComment(methodComments),
                        ResponseType  = responseType,
                    },

                    ParameterDescriptions = description.ParameterDescriptions.Select(d =>
                    {
                        var parameterBinder = GetParameterBinder(d.Source);
                        var parameterType   = d.ParameterDescriptor.ParameterType;
                        if ((parameterBinder == ParameterBinder.FromQuery || parameterBinder == ParameterBinder.FromUri) && !TypeHelper.IsValueType(parameterType) && !TypeHelper.IsNullablePremitive(parameterType))
                        {
                            throw new ArgumentException($"Not support ParameterBinder {parameterBinder} with a class parameter {parameterType.ToString()}.");
                        }

                        return(new ParameterDescription()
                        {
                            Documentation = DocCommentHelper.GetParameterComment(methodComments, d.Name),
                            Name = d.Name,
                            ParameterDescriptor = new ParameterDescriptor()
                            {
                                ParameterName = d.ParameterDescriptor.Name,
                                ParameterType = parameterType,
                                ParameterBinder = parameterBinder,
                            }
                        });
                    }).ToArray(),
                };

                return(dr);
            }
            catch (ArgumentException ex)//Expected to be thrown from GetParameterBinder()
            {
                var msg      = ex.Message;
                var errorMsg = $"Web API {controllerActionDescriptor.ControllerName}/{controllerActionDescriptor.ActionName} is defined with invalid parameters: {msg}";
                Trace.TraceError(errorMsg);
                throw new ArgumentException(errorMsg);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                throw;
            }
        }
 public ApiParameterContext(Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider metadataProvider, Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor actionDescriptor, System.Collections.Generic.IReadOnlyList <Microsoft.AspNetCore.Routing.Template.TemplatePart> routeParameters)
 {
 }
 public System.Collections.Generic.ICollection <Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseType> GetApiResponseTypes(Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor action)
 {
     throw null;
 }