优点:简化api调用过程,WebApiClient.AOT.dll中的IHttpApi接口
缺点:只适用于内部服务之间的调用(没有验证过程)
1.继承IHttpAPi接口
public interface IService: IHttpApi { [HttpGet("api/getvalue/{id}")] Task<ValueModel> GetValue([PathQuery]Guid id); [HttpGet("api/getallvalue")] Task<ValueModel[]> GetValues(); }
2.在startup中注入
services.AddSingleton(serviceProvider => HttpApiClient.Create<IService>(serviceProvider.GetRequiredService<ApiConfig>()));
3.在ApiConfig配置api地址
public class ApiConfig : HttpApiConfig { public ApiConfig(IConfiguration configuration, ILoggerFactory loggingFactory) { var httpHost = configuration["apiurl"];//apiurl在配置文件中 HttpHost = new Uri(httpHost); } }
然后,就可以使用IService.GetValue(id)调用api/getvalue/{id}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/17025.html