///// <summary> ///// 获取ITaskOf(dataType)的构造器 ///// </summary> ///// <param name="dataType">泛型参数类型</param> ///// <returns></returns> //public static ConstructorInfo GetITaskConstructor(Type dataType) //{ // return typeof(ApiTaskOf<>) // .MakeGenericType(dataType) // .GetConstructor(new[] { typeof(HttpApiConfig), typeof(ApiActionDescriptor) }); //} /// <summary> /// 创建ApiTaskOf(T)的实例 /// </summary> /// <param name="httpApiConfig">http接口配置</param> /// <param name="apiActionDescriptor">api描述</param> /// <returns></returns> public static object CreateInstance(HttpApiConfig httpApiConfig, ApiActionDescriptor apiActionDescriptor) { var context = new ApiActionContext { ApiActionDescriptor = apiActionDescriptor, HttpApiConfig = httpApiConfig, RequestMessage = new HttpApiRequestMessage { RequestUri = httpApiConfig.HttpHost }, ResponseMessage = null, Exception = null, Result = null }; context.PrepareRequestAsync(); context.ExecRequestAsync(); return(context.Result); }
/// <summary> /// 执行一次请求 /// </summary> /// <returns></returns> private async Task <TResult> RequestAsync() { var context = new ApiActionContext { ApiActionDescriptor = this.apiActionDescriptor, HttpApiConfig = this.httpApiConfig, RequestMessage = new HttpApiRequestMessage { RequestUri = this.httpApiConfig.HttpHost }, ResponseMessage = null, Exception = null, Result = null }; await context.PrepareRequestAsync().ConfigureAwait(false); await context.ExecFiltersAsync(filter => filter.OnBeginRequestAsync).ConfigureAwait(false); var state = await context.ExecRequestAsync().ConfigureAwait(false); await context.ExecFiltersAsync(filter => filter.OnEndRequestAsync).ConfigureAwait(false); return(state ? (TResult)context.Result : throw context.Exception); }