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

WebApiClient与Asp.net core DI的结合的更多相关文章

  1. ASP.NET Core DI 手动获取注入对象

    ASP.NET Core DI 一般使用构造函数注入获取对象,比如在ConfigureServices配置注入后,通过下面方式获取: private IValueService _valueServi ...

  2. ASP.NET Core DI概述

    众所周知,ASP.NET Core有一个DI框架,应用程序启动时初始化. 预定义依赖 1: IApplicationBuilder:提供了配置应用程序的请求管道机制 2:ILoggerFactory: ...

  3. DI in ASP.NET Core

    .NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...

  4. 用Scrutor来简化ASP.NET Core的DI注册

    目录 背景 Scrutor简介 Scrutor的简单使用 注册接口的实现类 注册类自身 重复注册处理策略 总结 相关文章 背景 在我们编写ASP.NET Core代码的时候,总是离不开依赖注入这东西. ...

  5. 声明式RESTful客户端在asp.net core中的应用

    1 声明式RESTful客户端 声明式服务调用的客户端,常见有安卓的Retrofit.SpringCloud的Feign等,.net有Refit和WebApiClient,这些客户端都是以java或. ...

  6. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组

    Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...

  7. 如何一秒钟从头构建一个 ASP.NET Core 中间件

    前言 其实地上本没有路,走的人多了,也便成了路. -- 鲁迅 就像上面鲁迅说的那样,其实在我们开发中间件的过程中,微软并没有制定一些策略或者文档来约束你如何编写一个中间件程序, 但是其中却存在者一些最 ...

  8. ASP.NET Core MVC 源码学习:Routing 路由

    前言 最近打算抽时间看一下 ASP.NET Core MVC 的源码,特此把自己学习到的内容记录下来,也算是做个笔记吧. 路由作为 MVC 的基本部分,所以在学习 MVC 的其他源码之前还是先学习一下 ...

  9. 全面理解 ASP.NET Core 依赖注入

    DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关于依赖注入的概念,身边有工作六七年的同事还个东西搞不清楚.另外再介绍一下.NET  Core的DI实现以及对实例 ...

随机推荐

  1. C++中将整型数与字符串型之间的类型转换

    整数转换成字符串类型: 方法一: 这里用到了char *itoa(int value, char *string, int radix); 函数当中参数为:int value 被转换的整数,char ...

  2. 【构造】UVa 11387 The 3-Regular Graph

    Description 输入n,构造一个n个点的无向图,使得每个点的度数都为3.不能有重边和自环,输出图或确定无解. Solution 如果n为奇数,奇数*3=奇数,度数为奇,必无解. 考虑我们怎么构 ...

  3. BZOJ_3427_Poi2013 Bytecomputer_DP

    BZOJ_3427_Poi2013 Bytecomputer_DP Description 给定一个{-1,0,1}组成的序列,你可以进行x[i]=x[i]+x[i-1]这样的操作,求最少操作次数使其 ...

  4. LCA 各种神奇的LCA优化方法

    LCA(Least Common Ancestors) 树上问题的一种. 朴素lca很简单啦,我就不多说了,时间复杂度n^2 1.倍增LCA 时间复杂度 nlongn+klogn 其实是一种基于朴素l ...

  5. Tomcat启动失败的几种解决办法

    1.重复映射 用Eclipse开发,新建了的servlet会有一个url-pattern声明: 这样就不需要在web.xml中添加映射,如果在web.xml中添加了这样一段: <servlet& ...

  6. ISCC2018(最新的考核解析)

    最近一直在做这个 ISCC2018,感觉可能自己只是一个新手吧!但是我会继续努力的,希望我的解题思路能够给你们带来一定的想法,我也希望自己能够在安全方面遇到更多志同道合的人! 其它题目可以看这里 1 ...

  7. centos7 启动docker失败的解决

    控制端使用yum install docker安装完成docker后启动docker失败,出现以下信息: Job for docker.service failed because the contr ...

  8. Redis+Restful 构造序列号和压力测试【后续】

    大家还记上篇博文https://www.cnblogs.com/itshare/p/8643508.html,测试redis构造流水号的tps是600多/1s. 这个速度显然不能体现redis 集群在 ...

  9. 《Thinking in Android 9.0 系统开发源码钻研录》

    最近打算把个人站点的博客文章同步到"博客园"! Thinking in Android -- "系统启动" [启动阶段] [相关文章] 状态 源码版本 init ...

  10. 国际知名CSS Reset 总结

    NO.01   CSS Tools: Reset CSS 网站:https://meyerweb.com/eric/tools/css/reset/ 优点:老牌,用的人多. /* http://mey ...