概述
HttpControllerDescriptor与HttpActionDescriptor
HttpControllerDescriptor封装了某个HttpController类型的元数据,我们可以将它视为某个HttpController类型的描述对象。HttpActionDescriptor也类似。上一篇中说到Abp将ApiControler与Action信息分别封装于DynamicApiControllerInfo,DynamicApiActionInfo。Abp种HttpControllerDescriptor与HttpActionDescriptor有新的实现DynamicHttpControllerDescriptor,DyanamicHttpActionDescriptor。
%26nbsp;
Action的执行
Action方法的执行最终实现在HttpActionDescriptor的ExecuteAsync方法中。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 } }); }
%26nbsp;
%26nbsp;
%26nbsp;
%26nbsp;
IHttpControllerSelector与IHttpActionSelector
在上一篇的开头提到了动态的WebApi的核心之一就是对原有的WebApi服务进行了替换。其中就包括IHttpControllerSelector与IHttpActionSelector,这两个接口的主要功能就是对url进行解析生成相应的HttpControllerDescriptor与HttpControllerDescriptor,并传给WebApi系统,再由WebApi系统去执行Action。
在上一篇中说到Abp将ApiController作DynamicApiControllerInfo缓存于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); }
%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}" ); } }
%26nbsp;
%26nbsp;
%26nbsp;
%26nbsp;
具体的解析就是:
Api/services/{ServicePrefix}/{ServiceName}/{Action}
在SimpleTaskSystem中一访问所有Tasks的url为
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(); }
%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, }
%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; } }
%26nbsp;
%26nbsp;
%26nbsp;
%26nbsp;
但作者好像并不打算这么做,在源代码中我也没有找到别的处理方式。
最后
以上就是霸气砖头为你收集整理的ABP之动态WebAPI(二)HttpControllerDescriptor与HttpActionDescriptorAction的执行IHttpControllerSelector与IHttpActionSelectorAbp的路由HttpVerb的全部内容,希望文章能够帮你解决ABP之动态WebAPI(二)HttpControllerDescriptor与HttpActionDescriptorAction的执行IHttpControllerSelector与IHttpActionSelectorAbp的路由HttpVerb所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复