我是靠谱客的博主 霸气砖头,最近开发中收集的这篇文章主要介绍ABP之动态WebAPI(二)HttpControllerDescriptor与HttpActionDescriptorAction的执行IHttpControllerSelector与IHttpActionSelectorAbp的路由HttpVerb,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

HttpControllerDescriptor与HttpActionDescriptor

HttpControllerDescriptor封装了某个HttpController类型的元数据,我们可以将它视为某个HttpController类型的描述对象。HttpActionDescriptor也类似。上一篇中说到AbpApiControlerAction信息分别封装于DynamicApiControllerInfoDynamicApiActionInfoAbpHttpControllerDescriptorHttpActionDescriptor有新的实现DynamicHttpControllerDescriptorDyanamicHttpActionDescriptor

%26nbsp;

Action的执行

Action方法的执行最终实现在HttpActionDescriptorExecuteAsync方法中。DyanamicHttpActionDescriptor也重写了ExecuteAsync,并且将Action的返回值类型定义为AjaxResponse

%26nbsp;

%26nbsp;

        public override Type ReturnType
        {
            get
            {
                return typeof(AjaxResponse);
            }
        }

        public override System.Threading.Tasks.Task%26lt;object%26gt; ExecuteAsync(HttpControllerContext controllerContext, System.Collections.Generic.IDictionary%26lt;string, object%26gt; arguments, System.Threading.CancellationToken cancellationToken)
        {
            return base
                .ExecuteAsync(controllerContext, arguments, cancellationToken)
                .ContinueWith(task =%26gt;
                {
                    try
                    {
                        if (task.Result == null)
                        {
                            return new AjaxResponse();
                        }

                        if (task.Result is AjaxResponse)
                        {
                            return task.Result;
                        }
                        
                        return new AjaxResponse(task.Result);
                    }
                    catch (AggregateException ex)
                    {
                        ex.InnerException.ReThrow();
                        throw; // The previous line will throw, but we need this to makes compiler happy
                    }
                });
        }
View Code

%26nbsp;

%26nbsp;

%26nbsp;

%26nbsp;

IHttpControllerSelector与IHttpActionSelector

在上一篇的开头提到了动态的WebApi的核心之一就是对原有的WebApi服务进行了替换。其中就包括IHttpControllerSelectorIHttpActionSelector,这两个接口的主要功能就是对url进行解析生成相应的HttpControllerDescriptorHttpControllerDescriptor,并传给WebApi系统,再由WebApi系统去执行Action

在上一篇中说到AbpApiControllerDynamicApiControllerInfo缓存于DynamicApiControllerManager中,在DynamicApiControllerManager中有一DynamicApiControllers的静态IDictionary属性。

private static readonly IDictionary%26lt;string, DynamicApiControllerInfo%26gt; DynamicApiControllers;

  

%26nbsp;

Abp系统的AbpHttpControllerSelector的中,根据url的解析出相应的服务名在DynamicApiControllerManager查找出DynamicApiControllerInfo.再生成DynamicHttpControllerDescriptor返回。

%26nbsp;

        public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            if (request != null)
            {
                var routeData = request.GetRouteData();
                if (routeData != null)
                {
                    string serviceNameWithAction;
                    if (routeData.Values.TryGetValue("serviceNameWithAction", out serviceNameWithAction) %26amp;%26amp; DynamicApiServiceNameHelper.IsValidServiceNameWithAction(serviceNameWithAction))
                    {
                        var serviceName = DynamicApiServiceNameHelper.GetServiceNameInServiceNameWithAction(serviceNameWithAction);
                        var controllerInfo = DynamicApiControllerManager.FindOrNull(serviceName);
                        if (controllerInfo != null)
                        {
                            var controllerDescriptor = new DynamicHttpControllerDescriptor(_configuration, controllerInfo.ServiceName, controllerInfo.Type, controllerInfo.Filters);
                            controllerDescriptor.Properties["__AbpDynamicApiControllerInfo"] = controllerInfo;
                            return controllerDescriptor;
                        }
                    }
                }
            }

            return base.SelectController(request);
        }
View Code

%26nbsp;

%26nbsp;

%26nbsp;

DynamicApiControllerInfo中有IDictionary%26lt;string, DynamicApiActionInfo%26gt; Actions字典所以获取Action也就十分方便。

%26nbsp;

%26nbsp;

Abp的路由

Abp定义了自己的路由格式

%26nbsp;

    public static class DynamicApiRouteConfig
    {
        /// %26lt;summary%26gt;
        /// Registers dynamic api controllers
        /// %26lt;/summary%26gt;
        public static void Register()
        {
            //Dynamic Web APIs (with area name)
            GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                name: "AbpDynamicWebApi",
                routeTemplate: "api/services/{*serviceNameWithAction}"
                );
        }
    }
View Code

%26nbsp;

%26nbsp;

%26nbsp;

%26nbsp;

具体的解析就是:

Api/services/{ServicePrefix}/{ServiceName}/{Action}

SimpleTaskSystem中一访问所有Tasksurl

http://localhost:6247/api/services/tasksystem/task/GetTasks

%26nbsp;

ServicePrefix:服务前缀,由开发者自己定义

SimpleTaskSystem

    [DependsOn(typeof(AbpWebApiModule))] //We declare depended modules explicitly
    public class SimpleTaskSystemWebApiModule : AbpModule
    {
        public override void Initialize()
        {
            //This code is used to register classes to dependency injection system for this assembly using conventions.
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            //Creating dynamic Web Api Controllers for application services.
            //Thus, 'web api layer' is created automatically by ABP.

            DynamicApiControllerBuilder
                .ForAll%26lt;IApplicationService%26gt;(Assembly.GetAssembly(typeof (SimpleTaskSystemApplicationModule)), "tasksystem")
                .Build();
        }
    }

  

ServiceName:服务名,由服务接口名解析而成。具体规则在BatchApiControllerBuilder. GetConventionalServiceName

%26nbsp;

        public static string GetConventionalServiceName(Type type)
        {
            var typeName = type.Name;

            if (typeName.EndsWith("ApplicationService"))
            {
                typeName = typeName.Substring(0, typeName.Length - "ApplicationService".Length);
            }
            else if (typeName.EndsWith("AppService"))
            {
                typeName = typeName.Substring(0, typeName.Length - "AppService".Length);
            }
            else if (typeName.EndsWith("Service"))
            {
                typeName = typeName.Substring(0, typeName.Length - "Service".Length);
            }

            if (typeName.Length %26gt; 1 %26amp;%26amp; typeName.StartsWith("I") %26amp;%26amp; char.IsUpper(typeName, 1))
            {
                typeName = typeName.Substring(1);
            }

            return typeName.ToCamelCase();
        }
View Code

%26nbsp;

%26nbsp;

%26nbsp;

Action:即为方法名

%26nbsp;

HttpVerb

Apb定义的四种Http访问方式

    [Flags]
    public enum HttpVerb
    {
        /// %26lt;summary%26gt;
        /// GET
        /// %26lt;/summary%26gt;
        Get,

        /// %26lt;summary%26gt;
        /// POST
        /// %26lt;/summary%26gt;
        Post,

        /// %26lt;summary%26gt;
        /// PUT
        /// %26lt;/summary%26gt;
        Put,

        /// %26lt;summary%26gt;
        /// DELETE
        /// %26lt;/summary%26gt;
        Delete,
    }
View Code

%26nbsp;

%26nbsp;

原本作者是定义了HttpVerb的处理方式,即对是通过方法名进行的分析。

%26nbsp;

    /// %26lt;summary%26gt;
    /// NOTE: This is not used (as all members are private)
    /// %26lt;/summary%26gt;
    internal static class DynamicApiVerbHelper
    {
        private static HttpVerb GetConventionalVerbForMethodName(string methodName)
        {
            if (methodName.StartsWith("Get"))
            {
                return HttpVerb.Get;
            }

            if (methodName.StartsWith("Update") || methodName.StartsWith("Put"))
            {
                return HttpVerb.Put;
            }

            if (methodName.StartsWith("Delete") || methodName.StartsWith("Remove"))
            {
                return HttpVerb.Delete;
            }

            if (methodName.StartsWith("Create") || methodName.StartsWith("Post"))
            {
                return HttpVerb.Post;
            }

            return GetDefaultHttpVerb();
        }

        private static HttpVerb GetDefaultHttpVerb()
        {
            return HttpVerb.Post;
        }
    }
View Code

%26nbsp;

%26nbsp;

%26nbsp;

%26nbsp;

但作者好像并不打算这么做,在源代码中我也没有找到别的处理方式。







最后

以上就是霸气砖头为你收集整理的ABP之动态WebAPI(二)HttpControllerDescriptor与HttpActionDescriptorAction的执行IHttpControllerSelector与IHttpActionSelectorAbp的路由HttpVerb的全部内容,希望文章能够帮你解决ABP之动态WebAPI(二)HttpControllerDescriptor与HttpActionDescriptorAction的执行IHttpControllerSelector与IHttpActionSelectorAbp的路由HttpVerb所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部