From 0b69f5d51d9b7ccd10e9b687e6f6b541ba8c195b Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 3 Oct 2019 10:44:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?perf(#I12XQJ):=20=E7=A7=BB=E9=99=A4=20Json.?= =?UTF-8?q?net=20=E5=85=A8=E9=9D=A2=E4=BD=BF=E7=94=A8=20System.Text.Json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Issue close #I12XQJ --- src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj | 6 +----- src/admin/Bootstrap.Admin/Startup.cs | 15 ++------------- .../Bootstrap.DataAccess.csproj | 2 +- .../Bootstrap.DataAccess/Helper/SMSHelper.cs | 16 ++++++++++++++-- src/client/Bootstrap.Client/Startup.cs | 9 +-------- 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj index a00ac7b0..795b8a09 100644 --- a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -4,7 +4,7 @@ netcoreapp3.0 true - + $(MSBuildProjectName).xml Windows @@ -13,16 +13,12 @@ - - - - diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs index 05ed94a3..b424fcd9 100644 --- a/src/admin/Bootstrap.Admin/Startup.cs +++ b/src/admin/Bootstrap.Admin/Startup.cs @@ -11,8 +11,6 @@ using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; using System; using System.Text.Encodings.Web; using System.Text.Unicode; @@ -59,11 +57,7 @@ namespace Bootstrap.Admin services.AddDbAdapter(); services.AddIPLocator(DictHelper.ConfigIPLocator); services.AddOnlineUsers(); - services.AddSignalR().AddNewtonsoftJsonProtocol(op => - { - op.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver(); - op.PayloadSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; - }); + services.AddSignalR().AddJsonProtocol(op => op.PayloadSerializerOptions.Configure()); services.AddSignalRExceptionFilterHandler(async (client, ex) => await client.SendMessageBody(ex).ConfigureAwait(false)); services.AddResponseCompression(); services.AddBootstrapAdminAuthentication().AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure); @@ -77,12 +71,7 @@ namespace Bootstrap.Admin { options.Filters.Add(); options.Filters.Add>(); - }).AddNewtonsoftJson(op => - { - op.SerializerSettings.ContractResolver = new DefaultContractResolver(); - op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; - JsonConvert.DefaultSettings = () => op.SerializerSettings; - }); + }).AddJsonOptions(op => op.JsonSerializerOptions.Configure()); services.AddApiVersioning(option => { option.DefaultApiVersion = new ApiVersion(1, 0); diff --git a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 44e0cb6a..7702c68a 100644 --- a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -10,7 +10,7 @@ - + diff --git a/src/admin/Bootstrap.DataAccess/Helper/SMSHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/SMSHelper.cs index 3805513e..e76fb953 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/SMSHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/SMSHelper.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.WebUtilities; -using Newtonsoft.Json; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -8,6 +7,12 @@ using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Threading; +#if NETCOREAPP3_0 +using System.Text.Json; +#else +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +#endif namespace Bootstrap.DataAccess { @@ -38,7 +43,14 @@ namespace Bootstrap.DataAccess var url = QueryHelpers.AddQueryString("http://open.bluegoon.com/api/sms/sendcode", requestParameters); var req = await client.GetAsync(url); var content = await req.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject(content); +#if NETCOREAPP3_0 + var result = JsonSerializer.Deserialize(content, new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true + }); +#else + var result = JsonConvert.DeserializeObject(content, new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver() }); +#endif var ret = false; if (result.Code == "1") { diff --git a/src/client/Bootstrap.Client/Startup.cs b/src/client/Bootstrap.Client/Startup.cs index b5180699..94d41a00 100644 --- a/src/client/Bootstrap.Client/Startup.cs +++ b/src/client/Bootstrap.Client/Startup.cs @@ -9,8 +9,6 @@ using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; using System; namespace Bootstrap.Client @@ -62,12 +60,7 @@ namespace Bootstrap.Client { options.Filters.Add(); options.Filters.Add>(); - }).AddNewtonsoftJson(op => - { - op.SerializerSettings.ContractResolver = new DefaultContractResolver(); - op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; - JsonConvert.DefaultSettings = () => op.SerializerSettings; - }); + }).AddJsonOptions(op => op.JsonSerializerOptions.Configure()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. From f883028783b38bdc7e9b2eadafb3eafa7022ed0d Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 3 Oct 2019 15:52:20 +0800 Subject: [PATCH 2/3] =?UTF-8?q?refactor(#I12XQJ):=20api=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20System.Text.Json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Issue link #I12XQJ --- src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj | 2 +- .../Controllers/Api/DictsController.cs | 4 ++-- .../Controllers/Api/LoginController.cs | 11 +++++------ src/admin/Bootstrap.Admin/Startup.cs | 3 +-- .../Bootstrap.DataAccess.csproj | 6 +----- .../Bootstrap.Client.DataAccess.csproj | 1 + src/client/Bootstrap.Client/Bootstrap.Client.csproj | 3 +-- src/client/Bootstrap.Client/Startup.cs | 3 +-- .../UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs | 9 ++++----- .../Bootstrap.Admin/HttpClientExtensions.cs | 13 +++++++------ 10 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj index 795b8a09..99bdbfa7 100644 --- a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/DictsController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/DictsController.cs index 72c5ff0e..47b89c83 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/DictsController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/DictsController.cs @@ -1,4 +1,4 @@ -using Bootstrap.Admin.Query; +using Bootstrap.Admin.Query; using Bootstrap.DataAccess; using Bootstrap.Security; using Longbow.Web.Mvc; @@ -47,4 +47,4 @@ namespace Bootstrap.Admin.Controllers.Api return DictHelper.Delete(value); } } -} \ No newline at end of file +} diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs index 07148ca2..d86420cb 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs @@ -29,16 +29,15 @@ namespace Bootstrap.Admin.Controllers.Api /// /// JWT 登陆认证接口 /// - /// + /// /// [AllowAnonymous] [HttpPost] - public string Post([FromBody]JObject value) + public string Post([FromBody]User user) { - string token = null; - dynamic user = value; - string userName = user.userName; - string password = user.password; + var token = string.Empty; + string userName = user.UserName; + string password = user.Password; if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password)) { token = BootstrapAdminJwtTokenHandler.CreateToken(userName); diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs index b424fcd9..a26276eb 100644 --- a/src/admin/Bootstrap.Admin/Startup.cs +++ b/src/admin/Bootstrap.Admin/Startup.cs @@ -106,9 +106,8 @@ namespace Bootstrap.Admin app.UseStaticFiles(); app.UseAutoGenerateDatabase(); app.UseRouting(); - app.UseAuthentication(); + app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseAuthorization(); - app.UseBootstrapAdminAuthorization(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/')); app.UseBootstrapHealthChecks(); app.UseOnlineUsers(TraceHelper.Filter, TraceHelper.Save); diff --git a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 7702c68a..1fc309d9 100644 --- a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -4,13 +4,9 @@ netcoreapp3.0;netstandard2.0 - - - - - + diff --git a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index b9511984..3524c17d 100644 --- a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -6,6 +6,7 @@ + diff --git a/src/client/Bootstrap.Client/Bootstrap.Client.csproj b/src/client/Bootstrap.Client/Bootstrap.Client.csproj index a8bbd990..cde6b779 100644 --- a/src/client/Bootstrap.Client/Bootstrap.Client.csproj +++ b/src/client/Bootstrap.Client/Bootstrap.Client.csproj @@ -6,10 +6,9 @@ - + - diff --git a/src/client/Bootstrap.Client/Startup.cs b/src/client/Bootstrap.Client/Startup.cs index 94d41a00..16586438 100644 --- a/src/client/Bootstrap.Client/Startup.cs +++ b/src/client/Bootstrap.Client/Startup.cs @@ -90,9 +90,8 @@ namespace Bootstrap.Client app.UseCookiePolicy(); app.UseRouting(); - app.UseAuthentication(); + app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseAuthorization(); - app.UseBootstrapAdminAuthorization(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseCacheManager(); app.UseOnlineUsers(callback: TraceHelper.Save); app.UseEndpoints(endpoints => diff --git a/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs b/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs index 972c93ca..92705c42 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs @@ -8,8 +8,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; -using Newtonsoft.Json; using System.Net.Http; +using System.Text.Json; using UnitTest; using Xunit; @@ -85,8 +85,7 @@ namespace Bootstrap.Admin.Api services.AddCacheManager(); services.AddConfigurationManager(); services.AddDbAdapter(); - var builder = services.AddHealthChecks(); - builder.AddCheck("db"); + services.AddHealthChecks().AddCheck("db"); services.AddControllers(); } @@ -101,10 +100,10 @@ namespace Bootstrap.Admin.Api }); app.UseEndpoints(builder => builder.MapHealthChecks("/Healths", new HealthCheckOptions() { - ResponseWriter = (context, report) => + ResponseWriter = async (context, report) => { context.Response.ContentType = "application/json"; - return context.Response.WriteAsync(JsonConvert.SerializeObject(new { report.Entries.Keys, Report = report })); + await context.Response.WriteAsync(JsonSerializer.Serialize(new { report.Status, report.TotalDuration })); }, ResultStatusCodes = { diff --git a/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs b/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs index ff87a6bb..dec13f3e 100644 --- a/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs +++ b/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs @@ -1,6 +1,7 @@ -using Newtonsoft.Json; +using Microsoft.Extensions.DependencyInjection; using System.Net.Http; using System.Net.Http.Headers; +using System.Text.Json; using System.Threading.Tasks; namespace Bootstrap.Admin @@ -18,7 +19,7 @@ namespace Bootstrap.Admin { var resp = await client.GetAsync(requestUri); var json = await resp.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(json); + return JsonSerializer.Deserialize(json, new JsonSerializerOptions().Configure()); } /// @@ -34,7 +35,7 @@ namespace Bootstrap.Admin { var resp = await client.PostAsJsonAsync(requestUri, t); var json = await resp.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(json); + return JsonSerializer.Deserialize(json, new JsonSerializerOptions().Configure()); } /// @@ -59,12 +60,12 @@ namespace Bootstrap.Admin public static async Task DeleteAsJsonAsync(this HttpClient client, string requestUri, TValue t) { var req = new HttpRequestMessage(HttpMethod.Delete, requestUri); - req.Content = new StringContent(JsonConvert.SerializeObject(t)); + req.Content = new StringContent(JsonSerializer.Serialize(t)); req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var resp = await client.SendAsync(req); var json = await resp.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(json); + return JsonSerializer.Deserialize(json, new JsonSerializerOptions().Configure()); } /// @@ -90,7 +91,7 @@ namespace Bootstrap.Admin { var resp = await client.PutAsJsonAsync(requestUri, t); var json = await resp.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(json); + return JsonSerializer.Deserialize(json, new JsonSerializerOptions().Configure()); } /// From ce363fcbbb4120fc4ba2cbc4d04178e86676dd1f Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 3 Oct 2019 15:53:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?build:=20=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=94=AF=E6=8C=81=E8=A6=86=E7=9B=96=20webapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- appveyor.test.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.test.ps1 b/appveyor.test.ps1 index ab5353bc..4fa1c7b0 100644 --- a/appveyor.test.ps1 +++ b/appveyor.test.ps1 @@ -38,7 +38,7 @@ function runUnitTest() { write-host "dotnet test test\UnitTest" -ForegroundColor Cyan - dotnet test test\UnitTest --filter "(FullyQualifiedName!~MySql) & (FullyQualifiedName!~Api.SqlServer) & (FullyQualifiedName!~Api.SQLite)" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include="[Bootstrap*]*" /p:ExcludeByFile="..\..\src\admin\Bootstrap.Admin\Program.cs%2c..\..\src\admin\Bootstrap.Admin\Startup.cs%2c..\..\src\admin\Bootstrap.Admin\HttpHeaderOperation.cs" /p:CoverletOutput=..\..\ + dotnet test test\UnitTest --filter "FullyQualifiedName!~MySql" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include="[Bootstrap*]*" /p:ExcludeByFile="..\..\src\admin\Bootstrap.Admin\Program.cs%2c..\..\src\admin\Bootstrap.Admin\Startup.cs%2c..\..\src\admin\Bootstrap.Admin\HttpHeaderOperation.cs" /p:CoverletOutput=..\..\ } function coverallUnitTest() {