From b181226c00c0ee0aa5d74a55e90c9bb9aaba1c28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E5=86=9B?= <510423039@qq.com>
Date: Sun, 4 Jul 2021 23:18:55 +0800
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=E5=85=A8?=
=?UTF-8?q?=E5=B1=80=E5=BC=82=E5=B8=B8=E6=8B=A6=E6=88=AA=E5=A4=84=E7=90=86?=
=?UTF-8?q?=E5=99=A8=20=E6=B7=BB=E5=8A=A0=E5=81=A5=E5=BA=B7=E6=A3=80?=
=?UTF-8?q?=E6=9F=A5Mysql=E5=92=8CRedis?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../aspnetcore/CompanyName.ProjectName.sln | 1 +
content/aspnetcore/NuGet.Config | 4 +-
...ompanyName.ProjectName.HttpApi.Host.csproj | 7 +-
...CompanyNameProjectNameHttpApiHostModule.cs | 57 ++++----
.../Dockerfile | 2 +-
.../Filters/ResultExceptionFilter.cs | 129 ++++++++++++++++++
.../appsettings.Ali.json | 61 ---------
.../appsettings.Staging.json | 61 ---------
.../appsettings.json | 2 +-
.../Attributes/DontWrapResultAttribute.cs | 12 ++
.../Attributes/WrapResult.cs | 31 +++++
.../Users/LoginAppService.cs | 1 -
.../appsettings.json | 2 +-
content/vue/.husky/_/husky.sh | 30 ++++
14 files changed, 244 insertions(+), 156 deletions(-)
create mode 100644 content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Extensions/Filters/ResultExceptionFilter.cs
delete mode 100644 content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/appsettings.Ali.json
delete mode 100644 content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/appsettings.Staging.json
create mode 100644 content/aspnetcore/src/CompanyName.ProjectName.Application/Attributes/DontWrapResultAttribute.cs
create mode 100644 content/aspnetcore/src/CompanyName.ProjectName.Application/Attributes/WrapResult.cs
create mode 100644 content/vue/.husky/_/husky.sh
diff --git a/content/aspnetcore/CompanyName.ProjectName.sln b/content/aspnetcore/CompanyName.ProjectName.sln
index 19573a88..fb9af0df 100644
--- a/content/aspnetcore/CompanyName.ProjectName.sln
+++ b/content/aspnetcore/CompanyName.ProjectName.sln
@@ -46,6 +46,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BC7E3BFE-3644-4893-8475-E543F3957F2A}"
ProjectSection(SolutionItems) = preProject
common.props = common.props
+ NuGet.Config = NuGet.Config
EndProjectSection
EndProject
Global
diff --git a/content/aspnetcore/NuGet.Config b/content/aspnetcore/NuGet.Config
index 0d5eaacd..a23bc850 100644
--- a/content/aspnetcore/NuGet.Config
+++ b/content/aspnetcore/NuGet.Config
@@ -1,10 +1,10 @@
-
+
-
+
\ No newline at end of file
diff --git a/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyName.ProjectName.HttpApi.Host.csproj b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyName.ProjectName.HttpApi.Host.csproj
index bbc3df36..0bd0bbe1 100644
--- a/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyName.ProjectName.HttpApi.Host.csproj
+++ b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyName.ProjectName.HttpApi.Host.csproj
@@ -26,6 +26,12 @@
+
+
+
+
+
+
@@ -35,7 +41,6 @@
-
diff --git a/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyNameProjectNameHttpApiHostModule.cs b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyNameProjectNameHttpApiHostModule.cs
index bc940c9b..5718bd32 100644
--- a/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyNameProjectNameHttpApiHostModule.cs
+++ b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/CompanyNameProjectNameHttpApiHostModule.cs
@@ -25,9 +25,6 @@ using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
-using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
-using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Bundling;
-using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Auditing;
using Volo.Abp.Autofac;
@@ -48,7 +45,6 @@ namespace CompanyNameProjectName
typeof(AbpAspNetCoreMultiTenancyModule),
typeof(CompanyNameProjectNameApplicationModule),
typeof(CompanyNameProjectNameEntityFrameworkCoreDbMigrationsModule),
- typeof(AbpAspNetCoreMvcUiBasicThemeModule),
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
typeof(AbpAccountWebIdentityServerModule),
typeof(AbpAspNetCoreSerilogModule),
@@ -76,7 +72,6 @@ namespace CompanyNameProjectName
{
var configuration = context.Services.GetConfiguration();
ConfigureOptions(context);
- ConfigureBundles();
ConfigureUrls(configuration);
ConfigureConventionalControllers();
ConfigureJwtAuthentication(context, configuration);
@@ -88,6 +83,7 @@ namespace CompanyNameProjectName
ConfigureCache(context.Services);
ConfigureAuditLog();
ConfigureHangfire(context.Services);
+ ConfigureHealthChecks(context);
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
@@ -101,12 +97,6 @@ namespace CompanyNameProjectName
}
app.UseAbpRequestLocalization();
-
- if (!env.IsDevelopment())
- {
- app.UseErrorPage();
- }
-
app.UseCorrelationId();
app.UseStaticFiles();
app.UseRouting();
@@ -114,7 +104,6 @@ namespace CompanyNameProjectName
app.UseAuthentication();
app.UseJwtTokenMiddleware();
app.UseAuthorization();
-
app.UseSwagger();
app.UseAbpSwaggerUI(c =>
{
@@ -131,10 +120,27 @@ namespace CompanyNameProjectName
});
app.UseConfiguredEndpoints();
-
+ app.UseEndpoints(endpoints =>
+ {
+ endpoints.MapHealthChecks("/health");
+ });
}
#region �有方法
+
+
+ private void ConfigureHealthChecks(ServiceConfigurationContext context)
+ {
+
+ var redisConnectionString =
+ context.Services.GetConfiguration().GetValue("Cache:Redis:ConnectionString")
+ +",defaultdatabase="
+ + context.Services.GetConfiguration().GetValue("Cache:Redis:DatabaseId", 1);
+
+ var mysqlConnectionString = context.Services.GetConfiguration().GetConnectionString("Default");
+ context.Services.AddHealthChecks().AddRedis(redisConnectionString).AddMySql(mysqlConnectionString);
+ }
+
///
/// �置options
///
@@ -152,8 +158,8 @@ namespace CompanyNameProjectName
{
Configure(options =>
{
- options.IsEnabled = true;
- options.EntityHistorySelectors.AddAllEntities();
+ options.IsEnabled = true;
+ options.EntityHistorySelectors.AddAllEntities();
options.ApplicationName = "CompanyName.ProjectName";
});
}
@@ -187,21 +193,18 @@ namespace CompanyNameProjectName
options.SendExceptionsDetailsToClients = true;
});
}
- }
-
-
- private void ConfigureBundles()
- {
- Configure(options =>
+ // 如果è¦�求返回值æ£ç¡®å’Œå¼‚å¸¸æ ¼å¼�ä¸€è‡´æ·»åŠ æ¬¡ç‰¹æ€§
+ context.Services.AddMvc(options =>
{
- options.StyleBundles.Configure(
- BasicThemeBundles.Styles.Global,
- bundle => { bundle.AddFiles("/global-styles.css"); }
- );
+ options.Filters.Add(typeof(ResultExceptionFilter));
});
+
}
+
+
+
private void ConfigureUrls(IConfiguration configuration)
{
Configure(options =>
@@ -231,7 +234,7 @@ namespace CompanyNameProjectName
{
options.ConventionalControllers.Create(typeof(CompanyNameProjectNameApplicationModule).Assembly);
});
-
+
}
///
@@ -362,7 +365,7 @@ namespace CompanyNameProjectName
});
var redisConnectionString = services.GetConfiguration().GetSection("Cache:Redis:ConnectionString").Value;
- var redisDatabaseId = Convert.ToInt32(services.GetConfiguration().GetSection("Cache:Redis:DatabaseId").Value);
+ var redisDatabaseId = services.GetConfiguration().GetValue("Cache:Redis:DatabaseId", 1);
// �用Hangfire 并使用Redis作为�久化
services.AddHangfire(config =>
diff --git a/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Dockerfile b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Dockerfile
index 46d94190..0f9737e9 100644
--- a/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Dockerfile
+++ b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Dockerfile
@@ -1,4 +1,4 @@
-FROM mcr.microsoft.com/dotnet/aspnet:5.0
+FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim
# ´´½¨Ä¿Â¼
RUN mkdir /apps
diff --git a/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Extensions/Filters/ResultExceptionFilter.cs b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Extensions/Filters/ResultExceptionFilter.cs
new file mode 100644
index 00000000..48fe1827
--- /dev/null
+++ b/content/aspnetcore/host/CompanyName.ProjectName.HttpApi.Host/Extensions/Filters/ResultExceptionFilter.cs
@@ -0,0 +1,129 @@
+using CompanyNameProjectName.Attributes;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Abstractions;
+using Microsoft.AspNetCore.Mvc.Filters;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Logging.Abstractions;
+using Microsoft.Extensions.Options;
+using System;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Volo.Abp;
+using Volo.Abp.AspNetCore.ExceptionHandling;
+using Volo.Abp.Authorization;
+using Volo.Abp.DependencyInjection;
+using Volo.Abp.Domain.Entities;
+using Volo.Abp.ExceptionHandling;
+using Volo.Abp.Http;
+using Volo.Abp.Json;
+using Volo.Abp.Validation;
+
+
+namespace CompanyNameProjectName.Extensions.Filters
+{
+ public class ResultExceptionFilter : IFilterMetadata, IAsyncExceptionFilter, ITransientDependency
+ {
+ public ILogger Logger { get; set; }
+
+ private readonly IExceptionToErrorInfoConverter _errorInfoConverter;
+ private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder;
+ private readonly IJsonSerializer _jsonSerializer;
+ private readonly AbpExceptionHandlingOptions _exceptionHandlingOptions;
+
+ public ResultExceptionFilter(
+ IExceptionToErrorInfoConverter errorInfoConverter,
+ IHttpExceptionStatusCodeFinder statusCodeFinder,
+ IJsonSerializer jsonSerializer,
+ IOptions exceptionHandlingOptions)
+ {
+ _errorInfoConverter = errorInfoConverter;
+ _statusCodeFinder = statusCodeFinder;
+ _jsonSerializer = jsonSerializer;
+ _exceptionHandlingOptions = exceptionHandlingOptions.Value;
+ Logger = NullLogger.Instance;
+ }
+
+ public async Task OnExceptionAsync(ExceptionContext context)
+ {
+ if (!ShouldHandleException(context))
+ {
+ return;
+ }
+
+
+ await HandleAndWrapException(context);
+ }
+
+ protected virtual bool ShouldHandleException(ExceptionContext context)
+ {
+ if (context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.GetCustomAttributes(typeof(DontWrapResultAttribute), true).Any())
+ {
+ return true;
+ }
+
+ if (context.ActionDescriptor.GetMethodInfo().GetCustomAttributes(typeof(DontWrapResultAttribute), true).Any())
+ {
+ return true;
+ }
+ return false;
+ }
+
+ protected virtual async Task HandleAndWrapException(ExceptionContext context)
+ {
+ //TODO: Trigger an AbpExceptionHandled event or something like that.
+
+ context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true");
+ var statusCode = (int)_statusCodeFinder.GetStatusCode(context.HttpContext, context.Exception);
+ context.HttpContext.Response.StatusCode = 200;
+
+ var remoteServiceErrorInfo = _errorInfoConverter.Convert(context.Exception, _exceptionHandlingOptions.SendExceptionsDetailsToClients);
+ remoteServiceErrorInfo.Code = context.HttpContext.TraceIdentifier;
+ remoteServiceErrorInfo.Message = SimplifyMessage(context.Exception);
+ var result = new WrapResult