diff --git a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index 316cf26a..00fc802f 100644 --- a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -5,11 +5,11 @@ + - diff --git a/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs b/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs index a7003f63..16c381f2 100644 --- a/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs +++ b/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs @@ -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 /// /// /// - 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().AuthHost.TrimEnd(new char[] { '/' }); - var url = $"{authHost}/api/Traces"; - client.BaseAddress = new Uri(url); - - _client = client; - } + public TraceHttpClient(HttpClient client) => _client = client; /// /// 提交数据到后台访问网页接口 diff --git a/src/client/Bootstrap.Client/Bootstrap.Client.csproj b/src/client/Bootstrap.Client/Bootstrap.Client.csproj index a52d5c64..1865e3e2 100644 --- a/src/client/Bootstrap.Client/Bootstrap.Client.csproj +++ b/src/client/Bootstrap.Client/Bootstrap.Client.csproj @@ -5,8 +5,7 @@ - - + diff --git a/src/client/Bootstrap.Client/HttpClientExtensions.cs b/src/client/Bootstrap.Client/HttpClientExtensions.cs new file mode 100644 index 00000000..ff22550e --- /dev/null +++ b/src/client/Bootstrap.Client/HttpClientExtensions.cs @@ -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((provider, client) => + { + client.Timeout = TimeSpan.FromSeconds(10); + client.DefaultRequestHeaders.Connection.Add("keep-alive"); + + // set auth + var context = provider.GetRequiredService(); + var cookieValues = context.HttpContext.Request.Cookies.Select(cookie => $"{cookie.Key}={cookie.Value}"); + client.DefaultRequestHeaders.Add("Cookie", cookieValues); + + var authHost = ConfigurationManager.Get().AuthHost.TrimEnd(new char[] { '/' }); + var url = $"{authHost}/api/Traces"; + client.BaseAddress = new Uri(url); + }); + return services; + } + } +} diff --git a/src/client/Bootstrap.Client/Startup.cs b/src/client/Bootstrap.Client/Startup.cs index a8f0e8db..5d1276e8 100644 --- a/src/client/Bootstrap.Client/Startup.cs +++ b/src/client/Bootstrap.Client/Startup.cs @@ -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(); services.AddSignalR().AddJsonProtocalDefault(); services.AddResponseCompression(); services.AddBootstrapAdminAuthentication();