Selaa lähdekoodia

优化httpclient请求

懒得勤快 4 vuotta sitten
vanhempi
sitoutus
98d411cb3d

+ 1 - 0
.gitignore

@@ -278,3 +278,4 @@ material_admin_v-1.5-2
 /src/Masuit.MyBlogs.Core/OneDrive.db
 /src/Masuit.MyBlogs.Core/App_Data/OneDrive.template.db
 /src/Masuit.MyBlogs.Core/App_Data/OneDrive.db
+/src/Masuit.MyBlogs.Core/.config

+ 21 - 5
src/Masuit.MyBlogs.Core/Controllers/ToolsController.cs

@@ -12,6 +12,7 @@ using Newtonsoft.Json;
 using Polly;
 using System;
 using System.Net.Http;
+using System.Threading;
 using System.Threading.Tasks;
 using TimeZoneConverter;
 
@@ -100,9 +101,17 @@ namespace Masuit.MyBlogs.Core.Controllers
                 return View(address);
             }
 
-            var s = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?location={lat},{lng}&output=json&pois=1&ak={AppConfig.BaiduAK}");
-            var physicsAddress = JsonConvert.DeserializeObject<PhysicsAddress>(s);
-            return View(physicsAddress);
+            var s = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?location={lat},{lng}&output=json&pois=1&ak={AppConfig.BaiduAK}", new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token).ContinueWith(t =>
+             {
+                 if (t.IsCompletedSuccessfully)
+                 {
+                     return JsonConvert.DeserializeObject<PhysicsAddress>(t.Result);
+                 }
+
+                 return new PhysicsAddress();
+             });
+
+            return View(s);
         }
 
         /// <summary>
@@ -134,8 +143,15 @@ namespace Masuit.MyBlogs.Core.Controllers
             }
 
             ViewBag.Address = addr;
-            var s = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}");
-            var physicsAddress = JsonConvert.DeserializeObject<PhysicsAddress>(s);
+            var physicsAddress = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}", new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token).ContinueWith(t =>
+             {
+                 if (t.IsCompletedSuccessfully)
+                 {
+                     return JsonConvert.DeserializeObject<PhysicsAddress>(t.Result);
+                 }
+
+                 return new PhysicsAddress();
+             });
             if (Request.Method.Equals(HttpMethods.Get))
             {
                 return View(physicsAddress?.AddressResult?.Location);

+ 3 - 0
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj

@@ -106,6 +106,9 @@
         <None Update="App_Data\mod.txt">
             <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         </None>
+        <None Update="App_Data\OneDrive.template.db">
+          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </None>
         <None Update="App_Data\whitelist.txt">
             <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         </None>

+ 5 - 5
src/Masuit.MyBlogs.Core/Startup.cs

@@ -146,21 +146,21 @@ namespace Masuit.MyBlogs.Core
             app.UseRequestIntercept(); //启用网站请求拦截
             app.SetupHangfire();
             app.UseResponseCaching().UseResponseCompression(); //启动Response缓存
-            app.UseMiddleware<TranslateMiddleware>();
+            app.UseWhen(c => !c.Request.Path.StartsWithSegments("/_") && c.Request.Scheme.StartsWith("http"), builder => builder.UseMiddleware<TranslateMiddleware>());
             //app.UseActivity();// 抽奖活动 
             app.UseRouting().UseEndpoints(endpoints =>
             {
                 endpoints.MapBlazorHub(options =>
                 {
-                    options.ApplicationMaxBufferSize = 0;
+                    options.ApplicationMaxBufferSize = 4194304;
                     options.LongPolling.PollTimeout = TimeSpan.FromSeconds(10);
-                    options.TransportMaxBufferSize = 0;
+                    options.TransportMaxBufferSize = 8388608;
                 });
                 endpoints.MapHub<MyHub>("/hubs", options =>
                 {
-                    options.ApplicationMaxBufferSize = 0;
+                    options.ApplicationMaxBufferSize = 4194304;
                     options.LongPolling.PollTimeout = TimeSpan.FromSeconds(10);
-                    options.TransportMaxBufferSize = 0;
+                    options.TransportMaxBufferSize = 8388608;
                 });
                 endpoints.MapControllers(); // 属性路由
                 endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); // 默认路由