WebApiClient与Asp.net core DI的结合

1 WebApiClient

一款基于HttpClient封装,只需要定义c#接口并修饰相关特性,即可异步调用远程http接口的客户端库

2 Http接口的注册与提供

2.1 声明远程端http接口

public interface IBaiduApi : IHttpApi
{
    [HttpGet("/s")]
    ITask<string> GetAsync(string word);
}

2.2 远程端http的注册

使用HttpClientFactory管理HttpClient的创建,利用AddTypedClient创建远程http接口的WebApiClient调用代理,同时给HttpApiConfig配置ServiceProvider实例。

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpApiTypedClient<IBaiduApi>().ConfigureHttpApiConfig((c, p) =>
    {
        c.HttpHost = new Uri("http://www.baidu.com/");
    });
}
/// <summary>
/// 添加HttpApiClient的别名HttpClient
/// </summary>
/// <typeparam name="TInterface">接口类型</typeparam>
/// <param name="services"></param>
/// <param name="configOptions">配置选项</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static IHttpClientBuilder AddHttpApiTypedClient<TInterface>(this IServiceCollection services, Action<HttpApiConfig, IServiceProvider> configOptions)
    where TInterface : class, IHttpApi
{
    if (configOptions == null)
    {
        throw new ArgumentNullException(nameof(configOptions));
    }

    return services
        .AddHttpClient<TInterface>()
        .AddTypedClient((httpClient, provider) =>
        {
            var httpApiConfig = new HttpApiConfig(httpClient)
            {
                ServiceProvider = provider
            };
            configOptions.Invoke(httpApiConfig, provider);
            return HttpApiClient.Create<TInterface>(httpApiConfig);
        });
}

2.3 远程端http接口的提供

可以使用构造器注入IBaiduApi或[FromServices]特性得到远程接口代理实例。

public class HomeController : Controller
{   
    // GET: /<controller>/
    public async Task<IActionResult> Index([FromServices] IBaiduApi baiduApi)
    {
        var html = await baiduApi.GetAsync("WebApiClient");
        return Content(html);
    }
}

3 WebApiClient过滤器的服务提供

3.1 在接口上使用自定义LogFilter

[LogFilter]
public interface IBaiduApi : IHttpApi
{
    [HttpGet("/s")]
    ITask<string> GetAsync(string word);
}

3.2 使用context.GetService获取服务实例

class LogFilter : ApiActionFilterAttribute
{
    public override Task OnBeginRequestAsync(ApiActionContext context)
    {
        var logger = context.GetService<ILoggerFactory>().CreateLogger("Baidu");
        logger.LogWarning($"request {context.ApiActionDescriptor.Name} {context.RequestMessage.RequestUri}");

        return base.OnBeginRequestAsync(context);
    }
}

3.3 日志服务输出日志样例

warn: Baidu[0]
      request GetAsync http://www.baidu.com/s?word=WebApiClient

转载于:https://www.cnblogs.com/kewei/p/10437350.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值