예제 #1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            #region 配置MVC与SingalR中间件

            //MVC
            services.AddControllersWithViews(options =>
            {
                options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
            })

#if DEBUG
            .AddRazorRuntimeCompilation()
#endif

            .AddNewtonsoftJson(options =>
            {
                ////将所有枚举序列化为字符串
                options.SerializerSettings.Converters.Add(new StringEnumConverter());
            })
            ;

            // Add SingalR
            services.AddSignalR(options => { options.EnableDetailedErrors = true; });

            #endregion 配置MVC与SingalR中间件

            #region 配置前后端分离跨域

            // 配置前后端分离跨域
            services.AddCors(
                options => options.AddPolicy(
                    _defaultCorsPolicyName,
                    builder => builder
                    .WithOrigins(
                        // 在appsettings.json中可以包含多个跨域地址,由逗号隔开。
                        _appConfiguration["App:CorsOrigins"]
                        .Split(",", StringSplitOptions.RemoveEmptyEntries)
                        .Select(o => o.RemovePostFix("/"))
                        .ToArray()
                        )
                    .SetIsOriginAllowedToAllowWildcardSubdomains()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()

                    )
                );

            #endregion 配置前后端分离跨域

            #region 配置支付宝

            services.AddYoYoAlipay(() =>
            {
                var res = _appConfiguration.GetSection("Pay:Alipay").Get <AlipayOptions>();
                return(res);
            }, (fTFConfig) =>
            {
                if (fTFConfig == null)
                {
                    fTFConfig = new FTFConfig();
                }

                fTFConfig.QRCodeGenErrorImageFullPath = System.IO.Path.Combine(_env.WebRootPath, "imgs", "pay", "alipay_error.png");
                fTFConfig.QRCodeIconFullPath          = System.IO.Path.Combine(_env.WebRootPath, "imgs", "pay", "alipay.png");
            });

            #endregion 配置支付宝

            services.AddHttpClient();

            // Add Wchat
            SenparcWXConfigurer.AddWechat(services, _appConfiguration);

            IdentityRegistrar.Register(services);

            AuthConfigurer.Configure(services, _appConfiguration);

            // IdentityServer4 配置
            if (bool.Parse(_appConfiguration["IdentityServer:IsEnabled"]))
            {
                IdentityServerRegistrar.Register(services, _appConfiguration);
            }

            #region 配置SwaggerUI

            if (WebConsts.SwaggerUiEnabled)
            {
                //Swagger -启用此行以及Configure方法中的相关行,以启用Swagger UI
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1",
                                       new OpenApiInfo
                    {
                        Title          = "52ABP-PRO API",
                        Version        = "v1",
                        Description    = "52ABP-PRO 的动态WEBAPI管理端,可以进行测试和调用API。",
                        TermsOfService = new Uri("https://gitee.com/ABPCN/52abp-pro"),
                        Contact        = new OpenApiContact
                        {
                            Name  = "52abp.com",
                            Email = "*****@*****.**",
                            Url   = new Uri("https://www.52abp.com/")
                        },
                    });

                    // 使用 camel case 的枚举
                    //options.DescribeStringEnumsInCamelCase();

                    //使用相对路径获取应用程序所在目录
                    options.DocInclusionPredicate((docName, description) => true);
                    // 支持非body内容中的枚举
                    options.ParameterFilter <SwaggerEnumParameterFilter>();
                    // 对应client枚举转为字符串对应值
                    options.SchemaFilter <SwaggerEnumSchemaFilter>();
                    options.OperationFilter <SwaggerOperationIdFilter>();
                    options.OperationFilter <SwaggerOperationFilter>();
                    options.CustomDefaultSchemaIdSelector();

                    options.OrderActionsBy(x => x.RelativePath);
                    options.DescribeAllParametersInCamelCase();
                    ConfigApiDoc(options);
                });

                // 使用 newtonsoft.json 做swagger的序列化工具
                services.AddSwaggerGenNewtonsoftSupport();
            }

            #endregion 配置SwaggerUI

            if (WebConsts.HangfireDashboardEnabled)
            {
                // 启用hangfire
                services.AddHangfire(config =>
                {
                    config.UseSqlServerStorage(_appConfiguration.GetConnectionString("Default"));
                    // config.UseRecurringJob(typeof(RecurringJobService)); //注入Hnagfire的测试服务
                });
            }

            #region 配置健康检查服务

            //services.AddHealthChecks().AddSqlServer(_appConfiguration["ConnectionStrings:Default"]);
            //services.AddHealthChecksUI();

            if (bool.Parse(_appConfiguration["HealthChecks:HealthChecksEnabled"]))
            {
                services.AddYoyoCmsHealthCheck();

                var healthCheckUISection = _appConfiguration.GetSection("HealthChecks")?.GetSection("HealthChecksUI");

                if (bool.Parse(healthCheckUISection["HealthChecksUIEnabled"]))
                {
                    services.Configure <HealthChecksUISettings>(settings =>
                    {
                        healthCheckUISection.Bind(settings, c => c.BindNonPublicProperties = true);
                    });
                    services.AddHealthChecksUI().AddInMemoryStorage();
                }
            }

            #endregion 配置健康检查服务

            // 配置abp和依赖注入
            return(services.AddAbp <YoyoCmsTemplateWebHostModule>(options =>
            {
                // 配置log4net
                options.IocManager.IocContainer.AddFacility <LoggingFacility>(
                    f => f.UseAbpLog4Net().WithConfig("log4net.config"));
            }
                                                                  ));
        }