Skip to content

Commit

Permalink
减少从DI获取服务的次数
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Jan 23, 2021
1 parent b555a3d commit 1d4281e
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions WebApiClientCore/DependencyInjection/HttpApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ public static class HttpApiExtensions
/// <returns></returns>
public static IHttpClientBuilder AddHttpApi<THttpApi>(this IServiceCollection services) where THttpApi : class
{
services.AddWebApiClient();

var name = HttpApi.GetName(typeof(THttpApi));
services.NamedHttpApiType(name, typeof(THttpApi));

services.AddWebApiClient();
services.NamedHttpApiType(name, typeof(THttpApi));
services.TryAddSingleton(typeof(HttpApiProvider<>));
services.TryAddTransient(serviceProvider =>
{
var httpClient = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(name);
var httpApiOptions = serviceProvider.GetRequiredService<IOptionsMonitor<HttpApiOptions>>().Get(name);
var httpClientContext = new HttpClientContext(httpClient, serviceProvider, httpApiOptions, name);
var httpApiInterceptor = new HttpApiInterceptor(httpClientContext);
var httpApiActivator = serviceProvider.GetRequiredService<IHttpApiActivator<THttpApi>>();
return httpApiActivator.CreateInstance(httpApiInterceptor);
var httiApiProvider = serviceProvider.GetRequiredService<HttpApiProvider<THttpApi>>();
return httiApiProvider.CreateHttpApi(serviceProvider, name);
});

return services.AddHttpClient(name);
Expand Down Expand Up @@ -121,6 +117,49 @@ public static IHttpClientBuilder AddHttpApi(this IServiceCollection services, Ty
}



/// <summary>
/// 表示THttpApi提供者
/// </summary>
/// <typeparam name="THttpApi"></typeparam>
private class HttpApiProvider<THttpApi>
{
private readonly IHttpClientFactory httpClientFactory;
private readonly IOptionsMonitor<HttpApiOptions> httpApiOptionsMonitor;
private readonly IHttpApiActivator<THttpApi> httpApiActivator;

/// <summary>
/// THttpApi提供者
/// </summary>
/// <param name="httpClientFactory"></param>
/// <param name="httpApiOptionsMonitor"></param>
/// <param name="httpApiActivator"></param>
public HttpApiProvider(IHttpClientFactory httpClientFactory, IOptionsMonitor<HttpApiOptions> httpApiOptionsMonitor, IHttpApiActivator<THttpApi> httpApiActivator)
{
this.httpClientFactory = httpClientFactory;
this.httpApiOptionsMonitor = httpApiOptionsMonitor;
this.httpApiActivator = httpApiActivator;
}

/// <summary>
/// 创建THttpApi的实例
/// </summary>
/// <param name="serviceProvider">服务提供者</param>
/// <param name="name">名称</param>
/// <returns></returns>
public THttpApi CreateHttpApi(IServiceProvider serviceProvider, string name)
{
var httpClient = this.httpClientFactory.CreateClient(name);
var httpApiOptions = this.httpApiOptionsMonitor.Get(name);

var httpClientContext = new HttpClientContext(httpClient, serviceProvider, httpApiOptions, name);
var httpApiInterceptor = new HttpApiInterceptor(httpClientContext);

return this.httpApiActivator.CreateInstance(httpApiInterceptor);
}
}


/// <summary>
/// 定义httpApi的Builder的行为
/// </summary>
Expand Down

0 comments on commit 1d4281e

Please sign in to comment.