From 943a7344f6b995c683fdedde52279cfa641d3d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Thu, 9 Feb 2023 19:11:56 +0800 Subject: [PATCH] =?UTF-8?q?aspnetcore=E6=A8=A1=E5=9D=97=E8=BD=AC=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Builder/CurrentUserUseExtensions.cs} | 13 +++----- .../Builder}/ErrorHandExtensions.cs | 2 +- .../AspNetCore/Builder/SwaggerUseExtensons.cs | 16 ++++++---- .../CurrentUserAddExtensions.cs | 30 +++++++++++++++++++ .../Yi.Framework.AspNetCore.csproj | 4 +++ .../YiFrameworkAspNetCoreModule.cs | 26 ++++++++++++++++ .../Helper/AssemblyHelper.cs | 1 + .../Yi.Framework.Core/Helper/JsonHelper.cs | 15 ++++++++-- .../YiFrameworkCoreModule.cs | 4 +-- .../Services/CrudAppService.cs | 8 ++++- .../project/BBS/Yi.BBS.Web/YiBBSWebModule.cs | 2 ++ .../Yi.Template.Web/YiTemplateWebModule.cs | 2 ++ .../project/bbs/Yi.BBS.Web/YiBBSWebModule.cs | 2 ++ .../rbac/Yi.RBAC.Web/YiRBACWebModule.cs | 2 ++ .../Yi.Template.Web/YiTemplateWebModule.cs | 2 ++ 15 files changed, 108 insertions(+), 21 deletions(-) rename Yi.Framework.Net6/src/framework/{Yi.Framework.Core/Extensions/CurrentUserExtensions.cs => Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/CurrentUserUseExtensions.cs} (89%) rename Yi.Framework.Net6/src/framework/{Yi.Framework.Core/Extensions => Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder}/ErrorHandExtensions.cs (99%) create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/CurrentUserAddExtensions.cs create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/CurrentUserExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/CurrentUserUseExtensions.cs similarity index 89% rename from Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/CurrentUserExtensions.cs rename to Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/CurrentUserUseExtensions.cs index 9ea41d3f..f97c9fc0 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/CurrentUserExtensions.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/CurrentUserUseExtensions.cs @@ -12,23 +12,18 @@ using System.Threading.Tasks; using Yi.Framework.Core.Const; using Yi.Framework.Core.CurrentUsers; -namespace Yi.Framework.Core.Extensions +namespace Microsoft.AspNetCore.Builder { - public static class CurrentUserExtensions + public static class CurrentUserUseExtensions { - public static IServiceCollection AddCurrentUserServer(this IServiceCollection services) - { - return services.AddScoped(); - } - - public static IApplicationBuilder UseCurrentUserServer(this IApplicationBuilder app) { return app.UseMiddleware(); } - } + } + public class CurrentUserMiddleware { diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/ErrorHandExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/ErrorHandExtensions.cs similarity index 99% rename from Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/ErrorHandExtensions.cs rename to Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/ErrorHandExtensions.cs index 6421c847..50f0fbc9 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/ErrorHandExtensions.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/ErrorHandExtensions.cs @@ -9,7 +9,7 @@ using System.Text; using System.Threading.Tasks; using Yi.Framework.Core.Exceptions; -namespace Yi.Framework.Core.Extensions +namespace Microsoft.AspNetCore.Builder { internal class ExceptionModle diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerUseExtensons.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerUseExtensons.cs index 293100d9..cf37af7b 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerUseExtensons.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerUseExtensons.cs @@ -27,7 +27,8 @@ namespace AspNetCore.Microsoft.AspNetCore.Builder { foreach (var k in swaggerModels) { - c.SwaggerEndpoint(k.url, k.name); + + c.SwaggerEndpoint(k.Url, k.Name); } } @@ -38,12 +39,17 @@ namespace AspNetCore.Microsoft.AspNetCore.Builder } public class SwaggerModel { + public SwaggerModel(string name) + { + this.Name = name; + this.Url = "/swagger/v1/swagger.json"; + } public SwaggerModel(string url, string name) { - this.url = url; - this.name = name; + this.Url = url; + this.Name = name; } - public string url { get; set; } - public string name { get; set; } + public string Url { get; set; } + public string Name { get; set; } } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/CurrentUserAddExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/CurrentUserAddExtensions.cs new file mode 100644 index 00000000..801b8a48 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/CurrentUserAddExtensions.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Core.Const; +using Yi.Framework.Core.CurrentUsers; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class CurrentUserAddExtensions + { + public static IServiceCollection AddCurrentUserServer(this IServiceCollection services) + { + return services.AddScoped(); + } + + + + } + + + +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Yi.Framework.AspNetCore.csproj b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Yi.Framework.AspNetCore.csproj index 4c195f34..20d2b889 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Yi.Framework.AspNetCore.csproj +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/Yi.Framework.AspNetCore.csproj @@ -18,4 +18,8 @@ + + + + diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs new file mode 100644 index 00000000..4274dd1c --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using StartupModules; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.AspNetCore +{ + public class YiFrameworkAspNetCoreModule : IStartupModule + { + + public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context) + { + app.UseCurrentUserServer(); + } + + public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) + { + services.AddCurrentUserServer(); + } + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs index a8a70a0d..a1e9b0dc 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs @@ -38,6 +38,7 @@ namespace Yi.Framework.Core.Helper return assembly.GetTypes().Where(m => m.IsClass && className == null ? true : m.Name == className && spaceName == null ? true : m.Namespace == spaceName + && !m.Name.StartsWith("<>") ).ToList(); } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/JsonHelper.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/JsonHelper.cs index 5b06a2ea..87e29171 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/JsonHelper.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/JsonHelper.cs @@ -1,4 +1,6 @@ -using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using System; using System.Collections.Generic; using System.Text.Json; @@ -6,7 +8,14 @@ namespace Yi.Framework.Core.Helper { public class JsonHelper { - + public static string ObjToStr(T obj, string dateTimeFormat) + { + IsoDateTimeConverter timeConverter = new IsoDateTimeConverter() + { + DateTimeFormat = dateTimeFormat + }; + return Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.Indented, timeConverter); + } public static string ObjToStr(T obj) { @@ -28,7 +37,7 @@ namespace Yi.Framework.Core.Helper string result = String.Empty; try { - JsonSerializer.Serialize(""); + System.Text.Json.JsonSerializer.Serialize(""); System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/YiFrameworkCoreModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/YiFrameworkCoreModule.cs index 6a6f3257..6315a158 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/YiFrameworkCoreModule.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/YiFrameworkCoreModule.cs @@ -24,7 +24,7 @@ namespace Yi.Framework.Core //全局错误,需要靠前,放在此处无效 //app.UseErrorHandlingServer(); - app.UseCurrentUserServer(); + } public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) @@ -34,7 +34,7 @@ namespace Yi.Framework.Core //全盘扫描,自动依赖注入 services.AddAutoIocServer(); - services.AddCurrentUserServer(); + } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/CrudAppService.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/CrudAppService.cs index e7a1a7a1..e71b6b01 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/CrudAppService.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/CrudAppService.cs @@ -79,7 +79,13 @@ namespace Yi.Framework.Ddd.Services ReflexHelper.SetModelValue(nameof(IEntity.Id), SnowflakeHelper.NextId, entity); } } - + if (entity is IEntity entityForGuidId) + { + if (entityForGuidId.Id == Guid.Empty) + { + ReflexHelper.SetModelValue(nameof(IEntity.Id), new Guid(), entity); + } + } return Task.FromResult(entity); } diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs index 2fbfc82f..d317c9e8 100644 --- a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs +++ b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs @@ -8,10 +8,12 @@ using Yi.BBS.Sqlsugar; using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection; using Yi.Framework.Core.Autofac; using Yi.RBAC.Application; +using Yi.Framework.AspNetCore; namespace Yi.BBS.Web { [DependsOn( + typeof(YiFrameworkAspNetCoreModule), typeof(YiFrameworkCoreAutofacModule), typeof(YiBBSSqlsugarModule), typeof(YiBBSApplicationModule) diff --git a/Yi.Framework.Net6/src/project/Template/Yi.Template.Web/YiTemplateWebModule.cs b/Yi.Framework.Net6/src/project/Template/Yi.Template.Web/YiTemplateWebModule.cs index c7623b63..5ba64864 100644 --- a/Yi.Framework.Net6/src/project/Template/Yi.Template.Web/YiTemplateWebModule.cs +++ b/Yi.Framework.Net6/src/project/Template/Yi.Template.Web/YiTemplateWebModule.cs @@ -1,5 +1,6 @@ using AspNetCore.Microsoft.AspNetCore.Builder; using StartupModules; +using Yi.Framework.AspNetCore; using Yi.Framework.Auth.JwtBearer; using Yi.Framework.Core; using Yi.Framework.Core.Attributes; @@ -10,6 +11,7 @@ using Yi.Template.Sqlsugar; namespace Yi.Template.Web { [DependsOn( + typeof(YiFrameworkAspNetCoreModule), typeof(YiFrameworkCoreAutofacModule), typeof(YiTemplateSqlsugarModule), typeof(YiTemplateApplicationModule) diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/YiBBSWebModule.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/YiBBSWebModule.cs index 2fbfc82f..d317c9e8 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/YiBBSWebModule.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/YiBBSWebModule.cs @@ -8,10 +8,12 @@ using Yi.BBS.Sqlsugar; using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection; using Yi.Framework.Core.Autofac; using Yi.RBAC.Application; +using Yi.Framework.AspNetCore; namespace Yi.BBS.Web { [DependsOn( + typeof(YiFrameworkAspNetCoreModule), typeof(YiFrameworkCoreAutofacModule), typeof(YiBBSSqlsugarModule), typeof(YiBBSApplicationModule) diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/YiRBACWebModule.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/YiRBACWebModule.cs index f93033d8..435c95a1 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/YiRBACWebModule.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/YiRBACWebModule.cs @@ -1,5 +1,6 @@ using AspNetCore.Microsoft.AspNetCore.Builder; using StartupModules; +using Yi.Framework.AspNetCore; using Yi.Framework.Auth.JwtBearer; using Yi.Framework.Core; using Yi.Framework.Core.Attributes; @@ -10,6 +11,7 @@ using Yi.RBAC.Sqlsugar; namespace Yi.RBAC.Web { [DependsOn( + typeof(YiFrameworkAspNetCoreModule), typeof(YiFrameworkCoreAutofacModule), typeof(YiRBACSqlsugarModule), typeof(YiRBACApplicationModule) diff --git a/Yi.Framework.Net6/src/project/template/Yi.Template.Web/YiTemplateWebModule.cs b/Yi.Framework.Net6/src/project/template/Yi.Template.Web/YiTemplateWebModule.cs index c7623b63..5ba64864 100644 --- a/Yi.Framework.Net6/src/project/template/Yi.Template.Web/YiTemplateWebModule.cs +++ b/Yi.Framework.Net6/src/project/template/Yi.Template.Web/YiTemplateWebModule.cs @@ -1,5 +1,6 @@ using AspNetCore.Microsoft.AspNetCore.Builder; using StartupModules; +using Yi.Framework.AspNetCore; using Yi.Framework.Auth.JwtBearer; using Yi.Framework.Core; using Yi.Framework.Core.Attributes; @@ -10,6 +11,7 @@ using Yi.Template.Sqlsugar; namespace Yi.Template.Web { [DependsOn( + typeof(YiFrameworkAspNetCoreModule), typeof(YiFrameworkCoreAutofacModule), typeof(YiTemplateSqlsugarModule), typeof(YiTemplateApplicationModule)