using BookStore.Data; using BookStore.DataSeeder; using BookStore.Localization; using Hangfire; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Sanhe.Abp.AspNetCore.Mvc.Wrapper; using Sanhe.Abp.ExceptionHandling; using Sanhe.Abp.ExceptionHandling.Emailing; using Sanhe.Abp.Features.LimitValidation.Redis; using Sanhe.Abp.Hangfire.Dashboard; using Sanhe.Abp.Hangfire.Storage.PostgreSql; using Sanhe.Abp.LocalizationManagement; using Sanhe.Abp.LocalizationManagement.EntityFrameworkCore; using Sanhe.Abp.Wrapper; using StackExchange.Redis; using System; using System.Collections.Generic; using System.IO; using System.Linq; using Volo.Abp; using Volo.Abp.Account; using Volo.Abp.Account.Web; using Volo.Abp.AspNetCore.Authentication.JwtBearer; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.Localization; 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.AuditLogging.EntityFrameworkCore; using Volo.Abp.Autofac; using Volo.Abp.AutoMapper; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.PostgreSql; using Volo.Abp.FeatureManagement; using Volo.Abp.FeatureManagement.EntityFrameworkCore; using Sanhe.Abp.Identity; using Sanhe.Abp.Identity.EntityFrameworkCore; using Volo.Abp.IdentityServer.EntityFrameworkCore; using Volo.Abp.Localization; using Volo.Abp.Localization.ExceptionHandling; using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.PermissionManagement; using Volo.Abp.PermissionManagement.EntityFrameworkCore; using Volo.Abp.PermissionManagement.HttpApi; using Volo.Abp.PermissionManagement.Identity; using Volo.Abp.SettingManagement; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.Settings; using Volo.Abp.Swashbuckle; using Volo.Abp.TenantManagement; using Volo.Abp.TenantManagement.EntityFrameworkCore; using Volo.Abp.UI.Navigation.Urls; using Volo.Abp.Validation.Localization; using Volo.Abp.VirtualFileSystem; namespace BookStore; [DependsOn( // ABP Framework packages typeof(AbpAspNetCoreMvcModule), typeof(AbpAspNetCoreMultiTenancyModule), typeof(AbpAutofacModule), typeof(AbpAutoMapperModule), //typeof(AbpCachingStackExchangeRedisModule), typeof(AbpEntityFrameworkCorePostgreSqlModule), typeof(AbpAspNetCoreMvcUiBasicThemeModule), typeof(AbpSwashbuckleModule), typeof(AbpAspNetCoreAuthenticationJwtBearerModule), typeof(AbpAspNetCoreSerilogModule), // Account module packages typeof(AbpAccountApplicationModule), typeof(AbpAccountHttpApiModule), typeof(AbpAccountWebIdentityServerModule), // Identity module packages typeof(AbpPermissionManagementDomainIdentityModule), typeof(AbpIdentityApplicationModule), typeof(AbpIdentityHttpApiModule), typeof(AbpIdentityEntityFrameworkCoreModule), typeof(AbpIdentityServerEntityFrameworkCoreModule), // Audit logging module packages typeof(AbpAuditLoggingEntityFrameworkCoreModule), // Permission Management module packages typeof(AbpPermissionManagementApplicationModule), typeof(AbpPermissionManagementHttpApiModule), typeof(AbpPermissionManagementEntityFrameworkCoreModule), // Tenant Management module packages typeof(AbpTenantManagementApplicationModule), typeof(AbpTenantManagementHttpApiModule), typeof(AbpTenantManagementEntityFrameworkCoreModule), // Feature Management module packages typeof(AbpFeatureManagementApplicationModule), typeof(AbpFeatureManagementEntityFrameworkCoreModule), typeof(AbpFeatureManagementHttpApiModule), // Setting Management module packages typeof(AbpSettingManagementApplicationModule), typeof(AbpSettingManagementEntityFrameworkCoreModule), typeof(AbpSettingManagementHttpApiModule), typeof(AbpAspNetCoreMvcWrapperModule), typeof(AbpEmailingExceptionHandlingModule), typeof(AbpFeaturesValidationRedisModule), typeof(AbpHangfireDashboardModule), typeof(AbpHangfireStoragePostgreSqlModule), // LocalizationManagement typeof(AbpLocalizationManagementApplicationModule), typeof(AbpLocalizationManagementEntityFrameworkCoreModule), typeof(AbpLocalizationManagementHttpApiModule) )] public class BookStoreModule : AbpModule { /* Single point to enable/disable multi-tenancy */ private const bool IsMultiTenant = true; public override void PreConfigureServices(ServiceConfigurationContext context) { context.Services.PreConfigure(options => { options.AddAssemblyResource( typeof(BookStoreResource) ); }); } public override void ConfigureServices(ServiceConfigurationContext context) { var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); context.Services.AddAlwaysAllowAuthorization(); // wrap Configure(options => { options.IsEnabled = true; options.IgnoreNamespaces.Clear(); }); // limit Configure(options => { options.Configuration = "127.0.0.1:6379"; }); ConfigureExceptionHandling(); ConfigureBundles(); ConfigureMultiTenancy(); ConfigureUrls(configuration); ConfigureAutoMapper(); ConfigureSwaggerServices(context, configuration); ConfigureAutoApiControllers(); ConfigureVirtualFiles(hostingEnvironment); ConfigureLocalization(); ConfigureAuthentication(context.Services, configuration); ConfigureCors(context, configuration); //ConfigureDataProtection(context, configuration, hostingEnvironment); ConfigureEfCore(context); ConfigureSeedWorker(context.Services, hostingEnvironment.IsDevelopment()); } private void ConfigureExceptionHandling() { // 自定义需要处理的异常 Configure(options => { // 加入需要处理的异常类型 options.Handlers.Add(); options.Handlers.Add(); options.Handlers.Add(); options.Handlers.Add(); options.Handlers.Add(); options.Handlers.Add(); options.Handlers.Add(); options.Handlers.Add(); }); // 自定义需要发送邮件通知的异常类型 Configure(options => { // 是否发送堆栈信息 options.SendStackTrace = true; // 未指定异常接收者的默认接收邮件 // 请指定自己的邮件地址 // options.DefaultReceiveEmail = ""; }); } private void ConfigureBundles() { Configure(options => { options.StyleBundles.Configure( BasicThemeBundles.Styles.Global, bundle => { bundle.AddFiles("/global-styles.css"); } ); }); } private void ConfigureMultiTenancy() { Configure(options => { options.IsEnabled = IsMultiTenant; }); } private void ConfigureUrls(IConfiguration configuration) { Configure(options => { options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"].Split(',')); options.Applications["Angular"].RootUrl = configuration["App:ClientUrl"]; options.Applications["Angular"].Urls[AccountUrlNames.PasswordReset] = "account/reset-password"; }); } private void ConfigureAuthentication(IServiceCollection services, IConfiguration configuration) { services.AddAuthentication() .AddJwtBearer(options => { options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); options.Audience = "BookStore"; }); } private void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddAbpSwaggerGenWithOAuth( configuration["AuthServer:Authority"], new Dictionary { { "BookStore", "BookStore API" } }, options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "BookStore API", Version = "v1" }); options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); var currentPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; if (!currentPath.IsNullOrWhiteSpace()) { var allXml = Directory.GetFiles(currentPath, "*.xml", SearchOption.TopDirectoryOnly); foreach (var xml in allXml) { options.IncludeXmlComments(xml, true); } } }); } private void ConfigureLocalization() { Configure(options => { options.Resources .Add("zh-Hans") .AddBaseTypes(typeof(AbpValidationResource)) .AddVirtualJson("/Localization/BookStore"); options.DefaultResourceType = typeof(BookStoreResource); options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); options.Languages.Add(new LanguageInfo("en", "en", "English")); // dynamic localization options.Resources.AddDynamic(); }); Configure(options => { options.MapCodeNamespace("BookStore", typeof(BookStoreResource)); }); } private void ConfigureVirtualFiles(IWebHostEnvironment hostingEnvironment) { Configure(options => { options.FileSets.AddEmbedded(); if (hostingEnvironment.IsDevelopment()) { /* Using physical files in development, so we don't need to recompile on changes */ options.FileSets.ReplaceEmbeddedByPhysical(hostingEnvironment.ContentRootPath); } }); } private void ConfigureAutoApiControllers() { //Configure(options => //{ // options.ConventionalControllers.Create(typeof(BookStoreModule).Assembly); //}); } private void ConfigureAutoMapper() { Configure(options => { options.AddMaps(/* validate: true */); }); } private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddCors(options => { options.AddDefaultPolicy(builder => { builder .WithOrigins( configuration["App:CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToArray() ) .WithAbpExposedHeaders() .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); } private void ConfigureDataProtection( ServiceConfigurationContext context, IConfiguration configuration, IWebHostEnvironment hostingEnvironment) { var dataProtectionBuilder = context.Services.AddDataProtection().SetApplicationName("BookStore"); if (!hostingEnvironment.IsDevelopment()) { var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); dataProtectionBuilder.PersistKeysToStackExchangeRedis(redis, "BookStore-Protection-Keys"); } } private void ConfigureEfCore(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => { options.AddDefaultRepositories(includeAllEntities: true); }); Configure(options => { options.Configure(configurationContext => { configurationContext.UseNpgsql(); }); }); } private static void ConfigureSeedWorker(IServiceCollection services, bool isDevelopment = false) { if (isDevelopment) { services.AddHostedService(); } } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); app.ApplicationServices.GetService().Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans"; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 微软内置从三个地方获取本地化:QueryString、Cookie、Header:accept-heade // 浏览器中文的Culture字符串是zh-CN与Abp的zh-Hans不匹配 // 故而清除此provider app.UseAbpRequestLocalization( options => options.RequestCultureProviders.RemoveAll(provider => provider is AcceptLanguageHeaderRequestCultureProvider)); if (!env.IsDevelopment()) { app.UseErrorPage(); } app.UseCorrelationId(); app.UseStaticFiles(); app.UseRouting(); app.UseCors(); app.UseAuthentication(); app.UseJwtTokenMiddleware(); if (IsMultiTenant) { app.UseMultiTenancy(); } app.UseUnitOfWork(); app.UseIdentityServer(); app.UseAuthorization(); app.UseSwagger(); app.UseAbpSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "BookStore API"); var configuration = context.ServiceProvider.GetRequiredService(); options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); options.OAuthScopes("BookStore"); options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None); }); app.UseAuditing(); app.UseAbpSerilogEnrichers(); app.UseHangfireDashboard(); app.UseConfiguredEndpoints(); } }