Browse Source

feat: 重构Shared.Host.Microservice

pull/89/head
王军 3 years ago
parent
commit
bd1b811fb5
  1. 1
      aspnet-core/gateways/Lion.AbpPro.WebGateway/GlobalUsings.cs
  2. 3
      aspnet-core/modules/BasicManagement/host/Lion.AbpPro.BasicManagement.HttpApi.Host/BasicManagementHttpApiHostModule.cs
  3. 21
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs
  4. 4
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/AutoDeleteAfterSuccessAttribute.cs
  5. 2
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/RecurringJobsExtensions.cs
  6. 13
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Middlewares/AbpProApplicationBuilderExtensionsExtensions.cs
  7. 5
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/GlobalUsings.cs
  8. 5
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json
  9. 1
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Gateways/Lion.AbpPro.Shared.Hosting.Gateways.csproj
  10. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Gateways/Lion/AbpPro/SharedHostingGatewayModule.cs
  11. 6
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs
  12. 32
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion.AbpPro.Shared.Hosting.Microservices.csproj
  13. 122
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion/AbpPro/SharedHostingMicroserviceModule.cs
  14. 14
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs
  15. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Mvc/Filters/LionExceptionFilter.cs
  16. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Mvc/Filters/LionResultFilter.cs
  17. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/RequestLog/RequestLogMiddleware.cs
  18. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/Extensions/DependencyInjection/ServiceCollectionExtensions.cs
  19. 5
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Serilog/SerilogToEsExtensions.cs
  20. 130
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/SharedHostingMicroserviceModule.cs
  21. 21
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/EnumSchemaFilter.cs
  22. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/HiddenAbpDefaultApiFilter.cs
  23. 22
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swaggers/EnumSchemaFilter.cs
  24. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs

1
aspnet-core/gateways/Lion.AbpPro.WebGateway/GlobalUsings.cs

@ -4,7 +4,6 @@ global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
global using Lion.AbpPro.Shared.Hosting.Gateways;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Cors;
global using Microsoft.AspNetCore.Hosting;

3
aspnet-core/modules/BasicManagement/host/Lion.AbpPro.BasicManagement.HttpApi.Host/BasicManagementHttpApiHostModule.cs

@ -2,12 +2,11 @@ using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.DataProtection;
using Lion.AbpPro.BasicManagement.EntityFrameworkCore;
using Lion.AbpPro.Shared.Hosting.Microservices;
using Lion.AbpPro.Shared.Hosting.Microservices.Swaggers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using StackExchange.Redis;
using Microsoft.OpenApi.Models;
using Swagger;
using Swashbuckle.AspNetCore.SwaggerUI;
using Volo.Abp;
using Volo.Abp.AspNetCore.ExceptionHandling;

21
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs

@ -1,3 +1,4 @@
using Swagger;
using Volo.Abp.BackgroundJobs.Hangfire;
namespace Lion.AbpPro
@ -18,8 +19,7 @@ namespace Lion.AbpPro
)]
public class AbpProHttpApiHostModule : AbpModule
{
public override void OnPostApplicationInitialization(
ApplicationInitializationContext context)
public override void OnPostApplicationInitialization(ApplicationInitializationContext context)
{
// 应用程序初始化的时候注册hangfire
context.CreateRecurringJob();
@ -43,11 +43,14 @@ namespace Lion.AbpPro
{
var app = context.GetApplicationBuilder();
var configuration = context.GetConfiguration();
//app.UseRequestLog();
app.UseAbpRequestLocalization();
app.UseCorrelationId();
app.UseStaticFiles();
if (configuration.GetValue("MiniProfiler:Enabled", false))
{
app.UseMiniProfiler();
}
app.UseRouting();
app.UseCors(AbpProHttpApiHostConst.DefaultCorsPolicyName);
app.UseAuthentication();
@ -84,6 +87,8 @@ namespace Lion.AbpPro
}
#region 私有配置
private void ConfigureHangfire(ServiceConfigurationContext context)
{
Configure<AbpBackgroundJobOptions>(options => { options.IsJobExecutionEnabled = true; });
@ -94,7 +99,7 @@ namespace Lion.AbpPro
var delaysInSeconds = new[] { 10, 60, 60 * 3 }; // 重试时间间隔
const int Attempts = 3; // 重试次数
config.UseFilter(new AutomaticRetryAttribute() { Attempts = Attempts, DelaysInSeconds = delaysInSeconds });
config.UseFilter(new AutoDeleteAfterSuccessAttributer(TimeSpan.FromDays(7)));
config.UseFilter(new AutoDeleteAfterSuccessAttribute(TimeSpan.FromDays(7)));
config.UseFilter(new JobRetryLastFilter(Attempts));
});
}
@ -103,10 +108,14 @@ namespace Lion.AbpPro
/// 配置MiniProfiler
/// </summary>
private void ConfigureMiniProfiler(ServiceConfigurationContext context)
{
if (context.Services.GetConfiguration().GetValue("MiniProfiler:Enabled", false))
{
context.Services.AddMiniProfiler(options => options.RouteBasePath = "/profiler").AddEntityFramework();
}
}
/// <summary>
/// 配置JWT
/// </summary>
@ -199,7 +208,7 @@ namespace Lion.AbpPro
context.Services.Configure<IdentityOptions>(options => { options.Lockout = new LockoutOptions() { AllowedForNewUsers = false }; });
}
private static void ConfigureSwaggerServices(ServiceConfigurationContext context)
private void ConfigureSwaggerServices(ServiceConfigurationContext context)
{
context.Services.AddSwaggerGen(
options =>
@ -324,5 +333,7 @@ namespace Lion.AbpPro
options.IgnoredUrls.Add("/cap");
});
}
#endregion
}
}

4
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/AutoDeleteAfterSuccessAttributer.cs → aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/AutoDeleteAfterSuccessAttribute.cs

@ -1,10 +1,10 @@
namespace Lion.AbpPro.Extensions.Hangfire;
public class AutoDeleteAfterSuccessAttributer : JobFilterAttribute, IApplyStateFilter
public class AutoDeleteAfterSuccessAttribute : JobFilterAttribute, IApplyStateFilter
{
private readonly TimeSpan _deleteAfter;
public AutoDeleteAfterSuccessAttributer(TimeSpan timeSpan)
public AutoDeleteAfterSuccessAttribute(TimeSpan timeSpan)
{
_deleteAfter = timeSpan;
}

2
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/RecurringJobsExtensions.cs

@ -1,6 +1,6 @@
using Lion.AbpPro.Jobs;
namespace Lion.AbpPro.Extensions
namespace Lion.AbpPro.Extensions.Hangfire
{
public static class RecurringJobsExtensions
{

13
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Middlewares/AbpProApplicationBuilderExtensionsExtensions.cs

@ -1,13 +0,0 @@
namespace Microsoft.AspNetCore.Builder;
public static class AbpProApplicationBuilderExtensionsExtensions
{
/// <summary>
/// 记录请求响应日志
/// </summary>
/// <returns></returns>
public static IApplicationBuilder UseRequestLog(this IApplicationBuilder app)
{
return app.UseMiddleware<RequestLogMiddleware>();
}
}

5
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/GlobalUsings.cs

@ -17,11 +17,7 @@ global using Lion.AbpPro.EntityFrameworkCore;
global using Lion.AbpPro.Core;
global using Lion.AbpPro.Extensions;
global using Lion.AbpPro.Extensions.Hangfire;
global using Lion.AbpPro.Extensions.Middlewares;
global using Lion.AbpPro.MultiTenancy;
global using Lion.AbpPro.Shared.Hosting.Microservices;
global using Lion.AbpPro.Shared.Hosting.Microservices.Microsoft.AspNetCore.Builder;
global using Lion.AbpPro.Shared.Hosting.Microservices.Swaggers;
global using Magicodes.ExporterAndImporter.Core;
global using Magicodes.ExporterAndImporter.Excel;
global using Microsoft.AspNetCore.Authentication.JwtBearer;
@ -58,6 +54,7 @@ global using Volo.Abp.AspNetCore.Serilog;
global using Volo.Abp.Auditing;
global using Volo.Abp.Authorization;
global using Volo.Abp.BackgroundJobs;
global using Volo.Abp.BackgroundJobs.Hangfire;
global using Volo.Abp.Caching;
global using Volo.Abp.Caching.StackExchangeRedis;
global using Volo.Abp.DependencyInjection;

5
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json

@ -35,7 +35,7 @@
},
"ConnectionStrings": {
"Default": "Data Source=localhost;Port=3306;Database=LionAbpProDB;uid=root;pwd=1q2w3E*;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true",
"Hangfire": "localhost:6379,defaultdatabase=1"
"Hangfire": "localhost:6379,defaultdatabase=5"
},
"Redis": {
"Configuration": "localhost,password=1q2w3E*,defaultdatabase=1"
@ -66,5 +66,8 @@
"Enabled": false,
"Host": "http://localhost:8500",
"Service": "LionAbpPro-Service"
},
"MiniProfiler": {
"Enabled": true
}
}

1
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Gateways/Lion.AbpPro.Shared.Hosting.Gateways.csproj

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ocelot" />

2
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Gateways/SharedHostingGatewayModule.cs → aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Gateways/Lion/AbpPro/SharedHostingGatewayModule.cs

@ -1,4 +1,4 @@
namespace Lion.AbpPro.Shared.Hosting.Gateways
namespace Lion.AbpPro
{
[DependsOn(
typeof(AbpSwashbuckleModule),

6
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs

@ -5,9 +5,10 @@ global using System.Collections.Generic;
global using System.Linq;
global using System.Net;
global using System.Reflection;
global using System.Text;
global using System.Text.RegularExpressions;
global using Consul;
global using Lion.AbpPro.Core;
global using Lion.AbpPro.Shared.Hosting.Microservices.Microsoft.Extensions.DependencyInjection;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Cors;
global using Microsoft.AspNetCore.Http;
@ -24,6 +25,9 @@ global using Microsoft.Extensions.Options;
global using Microsoft.OpenApi.Any;
global using Microsoft.OpenApi.Models;
global using Newtonsoft.Json;
global using Serilog;
global using Serilog.Exceptions;
global using Serilog.Sinks.Elasticsearch;
global using Swashbuckle.AspNetCore.SwaggerGen;
global using Volo.Abp;
global using Volo.Abp.AspNetCore.ExceptionHandling;

32
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion.AbpPro.Shared.Hosting.Microservices.csproj

@ -2,20 +2,46 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>/</RootNamespace>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ocelot.Provider.Polly" />
<PackageReference Include="Ocelot.Provider.Consul" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" />
<PackageReference Include="Volo.Abp" />
<PackageReference Include="Volo.Abp.AspNetCore" />
<PackageReference Include="Volo.Abp.Autofac" />
<PackageReference Include="Volo.Abp.Core" />
<PackageReference Include="Volo.Abp.Swashbuckle" />
<PackageReference Include="Swashbuckle.AspNetCore" />
<PackageReference Include="Volo.Abp.AspNetCore.MultiTenancy" />
<PackageReference Include="Volo.Abp.Caching.StackExchangeRedis" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Serilog.Exceptions" />
<PackageReference Include="Serilog.Extensions.Logging" />
<PackageReference Include="Serilog.Settings.Configuration" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" />
<PackageReference Include="Serilog.Sinks.File" />
<PackageReference Include="Volo.Abp.AspNetCore.Serilog" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" />
<PackageReference Include="Swashbuckle.AspNetCore" />
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" />
<PackageReference Include="MiniProfiler.EntityFrameworkCore" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\frameworks\src\Lion.AbpPro.Core\Lion.AbpPro.Core.csproj" />
</ItemGroup>

122
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion/AbpPro/SharedHostingMicroserviceModule.cs

@ -0,0 +1,122 @@
using Microsoft.AspNetCore.DataProtection;
using StackExchange.Redis;
using Volo.Abp.Caching;
namespace Lion.AbpPro;
[DependsOn(
typeof(AbpSwashbuckleModule),
typeof(AbpAutofacModule),
typeof(LionAbpProCoreModule))]
public class SharedHostingMicroserviceModule : AbpModule
{
private const string DefaultCorsPolicyName = "Default";
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
context.Services.AddConsulConfig(configuration);
ConfigureHealthChecks(context);
ConfigureLocalization();
ConfigureCors(context);
ConfigureConsul(context, configuration);
ConfigAntiForgery();
ConfigureAbpExceptions(context);
}
/// <summary>
/// 异常处理
/// </summary>
private void ConfigureAbpExceptions(ServiceConfigurationContext context)
{
context.Services.AddMvc
(
options =>
{
options.Filters.Add(typeof(LionExceptionFilter));
options.Filters.Add(typeof(LionResultFilter));
}
);
}
/// <summary>
/// 阻止跨站点请求伪造
/// https://docs.microsoft.com/zh-cn/aspnet/core/security/anti-request-forgery?view=aspnetcore-6.0
/// </summary>
private void ConfigAntiForgery()
{
Configure<AbpAntiForgeryOptions>(options => { options.AutoValidate = false; });
}
private void ConfigureConsul(ServiceConfigurationContext context,
IConfiguration configuration)
{
if (configuration.GetValue<bool>("Consul:Enabled", false))
{
context.Services.AddConsulConfig(configuration);
}
}
/// <summary>
/// 配置跨域
/// </summary>
private void ConfigureCors(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
context.Services.AddCors(options =>
{
options.AddPolicy(DefaultCorsPolicyName, builder =>
{
builder
.WithOrigins(
configuration["App:CorsOrigins"]
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/"))
.ToArray()
)
.WithAbpExposedHeaders()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
/// <summary>
/// 多语言配置
/// </summary>
private void ConfigureLocalization()
{
Configure<AbpLocalizationOptions>(options =>
{
options.Languages.Add(new LanguageInfo("ar", "ar", "العربية"));
options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština"));
options.Languages.Add(new LanguageInfo("en", "en", "English"));
options.Languages.Add(new LanguageInfo("en-GB", "en-GB", "English (UK)"));
options.Languages.Add(new LanguageInfo("fr", "fr", "Français"));
options.Languages.Add(new LanguageInfo("hu", "hu", "Magyar"));
options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português"));
options.Languages.Add(new LanguageInfo("ru", "ru", "Русский"));
options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe"));
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文"));
options.Languages.Add(new LanguageInfo("de-DE", "de-DE", "Deutsch", "de"));
options.Languages.Add(new LanguageInfo("es", "es", "Español", "es"));
});
}
/// <summary>
/// 健康检查
/// </summary>
/// <param name="context"></param>
private void ConfigureHealthChecks(ServiceConfigurationContext context)
{
// TODO 检查数据库和redis是否正常 AspNetCore.HealthChecks.Redis AspNetCore.HealthChecks.MySql
// context.Services.AddHealthChecks().AddRedis(redisConnectionString).AddMySql(connectString);
context.Services.AddHealthChecks();
}
}

14
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs

@ -1,8 +1,9 @@
namespace Lion.AbpPro.Shared.Hosting.Microservices.Microsoft.AspNetCore.Builder
using Microsoft.AspNetCore.RequestLog;
namespace Microsoft.AspNetCore.Builder
{
public static class ApplicationBuilderExtensions
{
public static string UseConsul(this IApplicationBuilder app)
{
var appLifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
@ -42,5 +43,14 @@ namespace Lion.AbpPro.Shared.Hosting.Microservices.Microsoft.AspNetCore.Builder
return consulServiceId;
}
/// <summary>
/// 记录请求响应日志
/// </summary>
/// <returns></returns>
public static IApplicationBuilder UseRequestLog(this IApplicationBuilder app)
{
return app.UseMiddleware<RequestLogMiddleware>();
}
}
}

2
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Mvc/Filters/LionExceptionFilter.cs

@ -1,6 +1,6 @@
using System.Text;
namespace Microservices.Microsoft.AspNetCore.Mvc.Filters;
namespace Microsoft.AspNetCore.Mvc.Filters;
public sealed class LionExceptionFilter : IAsyncExceptionFilter, ITransientDependency
{

2
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Mvc/Filters/LionResultFilter.cs

@ -1,4 +1,4 @@
namespace Microservices.Microsoft.AspNetCore.Mvc.Filters;
namespace Microsoft.AspNetCore.Mvc.Filters;
public class LionResultFilter : IResultFilter, ITransientDependency
{

2
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Middlewares/RequestLogMiddleware.cs → aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/RequestLog/RequestLogMiddleware.cs

@ -1,4 +1,4 @@
namespace Lion.AbpPro.Extensions.Middlewares;
namespace Microsoft.AspNetCore.RequestLog;
public class RequestLogMiddleware
{

2
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/Extensions/DependencyInjection/ServiceCollectionExtensions.cs

@ -1,4 +1,4 @@
namespace Lion.AbpPro.Shared.Hosting.Microservices.Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{

5
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/SerilogToEsExtensions.cs → aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Serilog/SerilogToEsExtensions.cs

@ -1,7 +1,4 @@
using Serilog.Exceptions;
using Serilog.Sinks.Elasticsearch;
namespace Lion.AbpPro.Extensions
namespace Serilog
{
public static class SerilogToEsExtensions
{

130
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/SharedHostingMicroserviceModule.cs

@ -1,130 +0,0 @@
using Microservices.Microsoft.AspNetCore.Mvc.Filters;
namespace Lion.AbpPro.Shared.Hosting.Microservices
{
[DependsOn(
typeof(AbpSwashbuckleModule),
typeof(AbpAutofacModule),
typeof(LionAbpProCoreModule))]
public class SharedHostingMicroserviceModule : AbpModule
{
private const string DefaultCorsPolicyName = "Default";
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
context.Services.AddConsulConfig(configuration);
ConfigureHealthChecks(context);
ConfigureLocalization();
ConfigureCors(context);
ConfigureUrls(configuration);
ConfigureConsul(context, configuration);
ConfigAntiForgery();
ConfigureAbpExceptions(context);
}
/// <summary>
/// 异常处理
/// </summary>
private void ConfigureAbpExceptions(ServiceConfigurationContext context)
{
context.Services.AddMvc
(
options =>
{
options.Filters.Add(typeof(LionExceptionFilter));
options.Filters.Add(typeof(LionResultFilter));
}
);
}
/// <summary>
/// 阻止跨站点请求伪造
/// https://docs.microsoft.com/zh-cn/aspnet/core/security/anti-request-forgery?view=aspnetcore-6.0
/// </summary>
private void ConfigAntiForgery()
{
Configure<AbpAntiForgeryOptions>(options => { options.AutoValidate = false; });
}
private void ConfigureConsul(ServiceConfigurationContext context,
IConfiguration configuration)
{
if (configuration.GetValue<bool>("Consul:Enabled", false))
{
context.Services.AddConsulConfig(configuration);
}
}
/// <summary>
/// 配置跨域
/// </summary>
private void ConfigureCors(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
context.Services.AddCors(options =>
{
options.AddPolicy(DefaultCorsPolicyName, builder =>
{
builder
.WithOrigins(
configuration["App:CorsOrigins"]
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/"))
.ToArray()
)
.WithAbpExposedHeaders()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
/// <summary>
/// 站点配置
/// </summary>
/// <param name="configuration"></param>
private void ConfigureUrls(IConfiguration configuration)
{
Configure<AppUrlOptions>(options => { options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; });
}
/// <summary>
/// 多语言配置
/// </summary>
private void ConfigureLocalization()
{
Configure<AbpLocalizationOptions>(options =>
{
options.Languages.Add(new LanguageInfo("ar", "ar", "العربية"));
options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština"));
options.Languages.Add(new LanguageInfo("en", "en", "English"));
options.Languages.Add(new LanguageInfo("en-GB", "en-GB", "English (UK)"));
options.Languages.Add(new LanguageInfo("fr", "fr", "Français"));
options.Languages.Add(new LanguageInfo("hu", "hu", "Magyar"));
options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português"));
options.Languages.Add(new LanguageInfo("ru", "ru", "Русский"));
options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe"));
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文"));
options.Languages.Add(new LanguageInfo("de-DE", "de-DE", "Deutsch", "de"));
options.Languages.Add(new LanguageInfo("es", "es", "Español", "es"));
});
}
/// <summary>
/// 健康检查
/// </summary>
/// <param name="context"></param>
private void ConfigureHealthChecks(ServiceConfigurationContext context)
{
// TODO 检查数据库和redis是否正常 AspNetCore.HealthChecks.Redis AspNetCore.HealthChecks.MySql
// context.Services.AddHealthChecks().AddRedis(redisConnectionString).AddMySql(connectString);
context.Services.AddHealthChecks();
}
}
}

21
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/EnumSchemaFilter.cs

@ -0,0 +1,21 @@
namespace Swagger;
/// <summary>
/// swagger 枚举映射,
/// 原因:前端代理生成枚举是数字
/// </summary>
public class EnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.IsEnum)
{
var array = new OpenApiArray();
array.AddRange(Enum.GetNames(context.Type).Select(n => new OpenApiString(n)));
// NSwag
schema.Extensions.Add("x-enumNames", array);
// Openapi-generator
schema.Extensions.Add("x-enum-varnames", array);
}
}
}

2
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swaggers/HiddenAbpDefaultApiFilter.cs → aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/HiddenAbpDefaultApiFilter.cs

@ -1,4 +1,4 @@
namespace Lion.AbpPro.Shared.Hosting.Microservices.Swaggers
namespace Swagger
{
/// <summary>
/// 在使用nswag的时候,原生默认的api导致生产的代理类存在问题

22
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swaggers/EnumSchemaFilter.cs

@ -1,22 +0,0 @@
namespace Lion.AbpPro.Shared.Hosting.Microservices.Swaggers
{
/// <summary>
/// swagger 枚举映射,
/// 原因:前端代理生成枚举是数字
/// </summary>
public class EnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.IsEnum)
{
var array = new OpenApiArray();
array.AddRange(Enum.GetNames(context.Type).Select(n => new OpenApiString(n)));
// NSwag
schema.Extensions.Add("x-enumNames", array);
// Openapi-generator
schema.Extensions.Add("x-enum-varnames", array);
}
}
}
}

2
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs

@ -1,4 +1,4 @@
namespace Lion.AbpPro.Shared.Hosting.Microservices.Volo.Abp
namespace Volo.Abp
{
/// <summary>
/// 修改Abp 返回状态码

Loading…
Cancel
Save