/// <summary> /// 具有生命周期的拦截器 /// </summary> /// <param name="httpApiConfig">httpApi配置</param> /// <param name="lifeTime">拦截器的生命周期</param> /// <param name="deactivateAction">失效回调</param> /// <exception cref="ArgumentNullException"></exception> public LifetimeInterceptor(HttpApiConfig httpApiConfig, TimeSpan lifeTime, Action <LifetimeInterceptor> deactivateAction) : base(httpApiConfig) { if (deactivateAction == null) { throw new ArgumentNullException(nameof(deactivateAction)); } this.lifeTime = lifeTime; this.tokenSource.Token.Register(() => { this.tokenSource.Dispose(); deactivateAction.Invoke(this); }, useSynchronizationContext: false); this.tokenSource.CancelAfter(lifeTime); }
///// <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); }
public static IHttpClientBuilder AddHttpApi <THttpApi>(this IServiceCollection services, Action <HttpApiOptions <THttpApi>, IServiceProvider> configureOptions) where THttpApi : class, IHttpApi { services .AddOptions <HttpApiOptions <THttpApi> >() .Configure(configureOptions); return(services .AddHttpClient(typeof(THttpApi).FullName) .AddTypedClient((httpClient, serviceProvider) => { var httpApiConfig = new HttpApiConfig(httpClient) { ServiceProvider = serviceProvider }; var httpApiOptions = serviceProvider.GetRequiredService <IOptions <HttpApiOptions <THttpApi> > >().Value; MergeOptions(httpApiConfig, httpApiOptions); return HttpApi.Create <THttpApi>(httpApiConfig); }) .ConfigurePrimaryHttpMessageHandler(() => new DefaultHttpClientHandler())); }
/// <summary> /// 创建LifetimeInterceptor /// </summary> /// <returns></returns> private LifetimeInterceptor CreateInterceptor() { var handler = this.handlerFunc?.Invoke() ?? new DefaultHttpClientHandler(); var httpApiConfig = new HttpApiConfig(handler, true); if (this.configAction != null) { this.configAction.Invoke(httpApiConfig); } if (this.keepCookieContainer == true) { var handlerContainer = httpApiConfig.HttpHandler.CookieContainer; Interlocked.CompareExchange(ref this.cookieContainer, handlerContainer, null); httpApiConfig.HttpHandler.CookieContainer = this.cookieContainer; } return(new LifetimeInterceptor( httpApiConfig, this.lifeTime, this.OnInterceptorDeactivate)); }
/// <summary> /// 创建实现了指定接口的HttpApiClient实例 /// </summary> /// <typeparam name="TInterface">请求接口类型</typeparam> /// <exception cref="ArgumentException"></exception> /// <exception cref="NotSupportedException"></exception> /// <returns></returns> public static TInterface Create <TInterface>() where TInterface : class, IDisposable { var config = new HttpApiConfig(); return(Create <TInterface>(config)); }
/// <summary> /// 创建实现了指定接口的HttpApiClient实例 /// </summary> /// <typeparam name="TInterface">请求接口类型</typeparam> /// <param name="httpApiConfig">接口配置</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="NotSupportedException"></exception> /// <returns></returns> public static TInterface Create <TInterface>(HttpApiConfig httpApiConfig) where TInterface : class { return(Create(typeof(TInterface), httpApiConfig) as TInterface); }
/// <summary> /// 创建TInterface接口的代理实例 /// </summary> /// <param name="httpApiConfig">httpApi配置</param> /// <returns></returns> protected virtual HttpApi CreateHttpApi(HttpApiConfig httpApiConfig) { return(HttpApi.Create(this.InterfaceType, httpApiConfig)); }
/// <summary> /// Api请求的异步任务 /// </summary> /// <param name="httpApiConfig">http接口配置</param> /// <param name="apiActionDescriptor">api描述</param> public ApiTaskOf(HttpApiConfig httpApiConfig, ApiActionDescriptor apiActionDescriptor) { this.httpApiConfig = httpApiConfig; this.apiActionDescriptor = apiActionDescriptor; }
/// <summary> /// http接口调用的拦截器 /// </summary> /// <param name="httpApiConfig">httpApi配置</param> /// <exception cref="ArgumentNullException"></exception> public LifeTimeTrackingInterceptor(HttpApiConfig httpApiConfig) : base(httpApiConfig) { }
/// <summary> /// 创建TInterface接口的代理实例 /// </summary> /// <param name="httpApiConfig">httpApi配置</param> /// <returns></returns> protected virtual TInterface CreateHttpApi(HttpApiConfig httpApiConfig) { return(HttpApiClient.Create <TInterface>(httpApiConfig)); }