WebApiClient
-
Published by xljiulang over 3 years ago
由于部分类型所在程序集已经变化,从1.x版本更新过来需要项目和项目依赖的包的WebApiClient和WebApiClient.Extensions都同时升级。
抽象与实现完全分离
- 接口与抽象类已放到新的WebApiClientCore.Abstractions程序集
- ApiActionDescriptor变成抽象类,增加IApiActionDescriptorProvider接口
- ApiActionInvoker设计为抽象类,增加IApiActionInvokerProvider接口
- ApiRequestContext等Context类不包含任何方法,之前的方法将放到WebApiClientCore程序集扩展出来
- 增加IHttpApiActivator接口,为代理类生成和实例创建方式提供可切换的实现
增强的语法分析
- 重构了WebApiClientCore.Analyzers,严谨的null检查;
- 减少不必要的重复执行代码逻辑块,获得更快的语法分析;
- 不需要显式为接口继承IHttpApi也能获得语法分析;
新增和增强的功能
- 增加IApiFilter类型,HttpApiOptions增加GlobalFilters的全局过滤器集合;
- 新增IWebApiClientBuilder IServiceCollection.AddWebApiClient()扩展,用于后续全局配置的扩展;
- 新增IWebApiClientBuilder IWebApiClientBuilder.UseJsonFirstApiActionDescriptor()扩展,缺省参数特性使用[JsonContent];
- 新增IWebApiClientBuilder IWebApiClientBuilder.UseSourceGeneratorHttpApiActivator()扩展,使用SourceGenerator编译时生成代理类代码;
- 在不配置HttpHost的情况下,[HttpGet(path)]现已支持带path参数;
- UriAttribute参数特性已经变成全能型,现已支持与[HttpGet(path)]等带path或者不带path结合使用;
- JsonContent类型增加JsonContent(object?, JsonSerializerOptions?, Encoding?)构造函数;
- FormContent类型增加FormContent(object?, KeyValueSerializerOptions?)构造函数;
- Extensions.OAuths删除了内部类型TokenProviderDescriptor,增加TokenProviderService<THttpApi,TTokenProvider>服务来管理TokenProvider;
- Extensions.OAuths删除了内部类型声明式的IOAuthClientApi接口,由OAuth2Client服务来原生实现请求,避免Extensions.SourceGenerator找不到代理类;
更快的性能和更少的内存分配
- 增加ValueStringBuilder类型,在多个关键性能的小内存申请,缓冲区由ArrayPool租赁改成stackalloc的ValueStringBuilder;
- 增加UriValue类型,用于实现连续的Uri参数占位符替换、增加query参数,减少string到Uri的转换;
- 增加HttpPath类型,[HttpGet]、[HttpPost]等执行时减少1次不必要的string到Uri的转换;
- Uri参数占位符替换,内存减低至原来的1/3;
- 创建代理类实例时,从DI获取服务由多次减少为1次;
- benchmark结果,cpu时间比1.5版本更少、内存分配也更低,post json请求cpu时间和内存分配稳低于Refit的1/3,get和post form时表现更好;
移除的功能
- HttpApi.Create()方法已经被删除,由DI管理的IHttpApiActivator来创建和激活接口的代理类;
- 序列化工具相关接口已被删除,增加xxxSerializer等静态序列化类:因为各序列化工具无法统一接口,WebApiClient只关注他们的Options而不是接口;