问题详情
在一个 ASP.NET Core 10 项目中,web api client 是基于 Refit 实现的
public interface IBlogPostClient
{
[Get("/v2/blogs/{blogId}/posts/{postId}")]
Task<ApiBlogPostDto> GetBlogPost(int blogId, int postId);
}
services.AddRefitClient<IBlogPostClient>()
.ConfigureHttpClient((sp, client) =>
{
client.BaseAddress = new Uri(options.Value.BaseAddress);
client.Timeout = TimeSpan.FromSeconds(10);
});
今天有个场景需要直接通过 HttpClient 调用 web api,请问如何通过 Refit 获取 HttpClient 实例?
回答
只需给接口添加类型为 HttpClient 名为 Client 的属性即可,Refit 会自动注入 HttpClient 实例到这个属性
public interface IBlogPostClient
{
[Get("/v2/blogs/{blogId}/posts/{postId}")]
Task<ApiBlogPostDto> GetBlogPost(int blogId, int postId);
HttpClient Client { get; }
}
版权:言论仅代表个人观点,不代表官方立场。转载请注明出处:https://www.stntk.com/question/2334.html
还没有评论呢,快来抢沙发~