refactor: 合并 HttpClient 注入

This commit is contained in:
Argo Zhang 2019-09-05 21:56:56 +08:00
parent 5cf7130e6d
commit 9a3b7a94db
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
5 changed files with 40 additions and 24 deletions

View File

@ -5,11 +5,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.13-preview-1" />
<PackageReference Include="Longbow.Cache" Version="2.2.15" />
<PackageReference Include="Longbow.Data" Version="2.3.7" />
<PackageReference Include="Longbow.Logging" Version="2.2.13" />
<PackageReference Include="Longbow.Web" Version="2.2.16" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.15" />
</ItemGroup>
</Project>

View File

@ -1,10 +1,7 @@
using Longbow.Configuration;
using Longbow.Web;
using Microsoft.AspNetCore;
using Longbow.Web;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Net.Http;
namespace Bootstrap.Client.DataAccess
@ -21,21 +18,7 @@ namespace Bootstrap.Client.DataAccess
/// </summary>
/// <param name="client"></param>
/// <param name="httpContextAccessor"></param>
public TraceHttpClient(HttpClient client, IHttpContextAccessor httpContextAccessor)
{
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.Connection.Add("keep-alive");
// set auth
var cookieValues = httpContextAccessor.HttpContext.Request.Cookies.Select(cookie => $"{cookie.Key}={cookie.Value}");
client.DefaultRequestHeaders.Add("Cookie", cookieValues);
var authHost = ConfigurationManager.Get<BootstrapAdminAuthenticationOptions>().AuthHost.TrimEnd(new char[] { '/' });
var url = $"{authHost}/api/Traces";
client.BaseAddress = new Uri(url);
_client = client;
}
public TraceHttpClient(HttpClient client) => _client = client;
/// <summary>
/// 提交数据到后台访问网页接口

View File

@ -5,8 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.12" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.15" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.16-preview-1" />
<PackageReference Include="Longbow.Configuration" Version="2.2.7" />
<PackageReference Include="Longbow.Logging" Version="2.2.13" />
<PackageReference Include="Microsoft.AspNetCore.App" />

View File

@ -0,0 +1,32 @@
using Bootstrap.Client.DataAccess;
using Longbow.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
namespace Microsoft.AspNetCore.Builder
{
internal static class HttpClientExtensions
{
public static IServiceCollection AddBootstrapHttpClient(this IServiceCollection services)
{
services.AddHttpClient("BootstrapAdmin", client => client.DefaultRequestHeaders.Connection.Add("keep-alive"));
services.AddHttpClient<TraceHttpClient>((provider, client) =>
{
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.Connection.Add("keep-alive");
// set auth
var context = provider.GetRequiredService<IHttpContextAccessor>();
var cookieValues = context.HttpContext.Request.Cookies.Select(cookie => $"{cookie.Key}={cookie.Value}");
client.DefaultRequestHeaders.Add("Cookie", cookieValues);
var authHost = ConfigurationManager.Get<BootstrapAdminAuthenticationOptions>().AuthHost.TrimEnd(new char[] { '/' });
var url = $"{authHost}/api/Traces";
client.BaseAddress = new Uri(url);
});
return services;
}
}
}

View File

@ -1,6 +1,8 @@
using Bootstrap.Client.DataAccess;
using Longbow.Configuration;
using Longbow.Web;
using Longbow.Web.SignalR;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
@ -11,6 +13,7 @@ using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Linq;
namespace Bootstrap.Client
{
@ -51,10 +54,9 @@ namespace Bootstrap.Client
services.AddConfigurationManager();
services.AddCacheManager();
services.AddDbAdapter();
services.AddHttpClient("BootstrapAdmin", client => client.DefaultRequestHeaders.Connection.Add("keep-alive"));
services.AddBootstrapHttpClient();
services.AddIPLocator(DictHelper.ConfigIPLocator);
services.AddOnlineUsers();
services.AddHttpClient<TraceHttpClient>();
services.AddSignalR().AddJsonProtocalDefault();
services.AddResponseCompression();
services.AddBootstrapAdminAuthentication();