Browse Source

允许 多次读取 request.Body

Roc 5 years ago
parent
commit
0f5089e9f2

+ 5 - 0
samples/WebApplicationSample/Controllers/WeChatPayNotifyController.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using Essensoft.AspNetCore.Payment.WeChatPay;
 using Essensoft.AspNetCore.Payment.WeChatPay.V2;
 using Essensoft.AspNetCore.Payment.WeChatPay.V2.Notify;
+using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Options;
 
@@ -29,6 +30,8 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
+                Request.EnableBuffering();
+
                 var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
                 if (notify.ReturnCode == WeChatPayCode.Success)
                 {
@@ -57,6 +60,8 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
+                Request.EnableBuffering();
+
                 var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
                 if (notify.ReturnCode == WeChatPayCode.Success)
                 {

+ 3 - 0
samples/WebApplicationSample/Controllers/WeChatPayV3NotifyController.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using Essensoft.AspNetCore.Payment.WeChatPay;
 using Essensoft.AspNetCore.Payment.WeChatPay.V3;
 using Essensoft.AspNetCore.Payment.WeChatPay.V3.Notify;
+using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Options;
 
@@ -29,6 +30,8 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
+                Request.EnableBuffering();
+
                 var notify = await _client.ExecuteAsync<WeChatPayTransactionsNotify>(Request, _optionsAccessor.Value);
                 if (notify.TradeState == WeChatPayTradeState.Success)
                 {

+ 6 - 2
src/Essensoft.AspNetCore.Payment.WeChatPay/V2/WeChatPayNotifyClient.cs

@@ -29,8 +29,12 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.V2
                 throw new ArgumentNullException(nameof(request));
             }
 
-            var body = await new StreamReader(request.Body, Encoding.UTF8).ReadToEndAsync();
-            return await ExecuteAsync<T>(body, options);
+            request.Body.Seek(0, SeekOrigin.Begin);
+            using (var reader = new StreamReader(request.Body, Encoding.UTF8))
+            {
+                var body = await reader.ReadToEndAsync();
+                return await ExecuteAsync<T>(body, options);
+            }
         }
 #endif
 

+ 6 - 3
src/Essensoft.AspNetCore.Payment.WeChatPay/V3/WeChatPayNotifyClient.cs

@@ -35,10 +35,13 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.V3
                 throw new ArgumentNullException(nameof(options));
             }
 
+            request.Body.Seek(0, SeekOrigin.Begin);
             var headers = GetWeChatPayHeadersFromRequest(request);
-            var body = await new StreamReader(request.Body, Encoding.UTF8).ReadToEndAsync();
-
-            return await ExecuteAsync<T>(headers, body, options);
+            using (var reader = new StreamReader(request.Body, Encoding.UTF8))
+            {
+                var body = await reader.ReadToEndAsync();
+                return await ExecuteAsync<T>(headers, body, options);
+            }
         }
 
         private WeChatPayHeaders GetWeChatPayHeadersFromRequest(Microsoft.AspNetCore.Http.HttpRequest request)