sunpengfei
6 天以前 d317a290c03d4c40687fcd01efc6c22304874e87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using Consul;
using FlexJobApi.Core;
using FlexJobApi.Core.Jobs;
using Furion;
using Furion.EventBus;
using Furion.Schedule;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FlexJobApi.Web.Entry
{
    [AppStartup(2)]
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHealthChecks();
 
            services.AddConfigurableOptions<WxmpOptions>();
            services.AddConfigurableOptions<AlipayOptions>();
            services.AddConfigurableOptions<AliyunOptions>();
            services.AddConfigurableOptions<BaiduOptions>();
 
            services.AddComponent<LogServiceComponent>();
 
            services.AddComponent<ConsulServiceComponent>();
 
            services.AddComponent<EventBusServiceComponent>();
 
            services.AddComponent<DistributedCacheServiceComponent>();
 
            services.AddHttpRemote(); 
            services.AddSingleton<AlipayUtils>();
            services.AddSingleton<AliyunSmsUtils>(); 
            services.AddSingleton<WxmpUtils>();
            services.AddScoped<SmsUtils>();
 
            services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(App.Assemblies.ToArray()));
 
            services.AddHostedService<XmlDocBuildHostedService>();
 
            services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
 
            services.AddCorsAccessor();
 
            services.AddHostedService<BuildDynamicControllersHostedService>();
 
            services.AddSchedule(options =>
            {
                options.BuildSqlType = SqlTypes.SqlServer;
                options.JobDetail.LogEnabled = true;
                //options.AddPersistence<DbJobPersistence>();
                options.AddJob<CalcTaskSatusByDateJob>(Triggers.Hourly());
                options.AddJob<CalcUserAgeByIdCardJob>(Triggers.Daily());
                options.AddJob<RefreshEnterpriseWalletStatusJob>(Triggers.PeriodMinutes(5));
                options.AddJob<RefreshEnterpriseWalletTransactionStatusJob>(Triggers.PeriodMinutes(2));
                options.AddJob<CompleteTaskSettlementT1Job>(Triggers.PeriodMinutes(2));
            });
 
            services.AddSpecificationDocuments(options =>
            {
                options.OperationFilter<CustomOperationIdFilter>();
                options.SchemaFilter<EnumSchemaFilter>();
            });
            services.AddSwaggerGenNewtonsoftSupport();
 
            services.AddMvcFilter<ResourceActionFilter>();
 
            services.AddControllers()
                    .AddNewtonsoftJson(options =>
                    {
                        options.SerializerSettings.Converters.Add(new EmptyStringToNullConverter());
                        //options.SerializerSettings.Converters.Add(new StringEnumConverter());
                    })
                    .AddFriendlyException()
                    .AddDataValidation()
                    .AddInjectWithUnifyResult<FriendlyResultProvider>();
 
        }
 
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
        {
            // 解析事件总线发布服务
            var eventPublisher = app.ApplicationServices.GetRequiredService<IEventPublisher>();
 
            // 订阅
            eventPublisher.OnExecuted += (sender, args) =>
            {
                Console.WriteLine($"事件 {args.Source.EventId} 执行状态:{args.Status},异常:{args.Exception},执行结果:{args.Result}");
            };
 
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
 
            app.UseUnifyResultStatusCodes();
 
            //app.UseHttpsRedirection();
 
            app.UseRouting();
 
            app.UseCorsAccessor();
 
            app.UseAuthentication();
            app.UseAuthorization();
 
            app.UseInject(string.Empty);
 
            app.UseStaticFiles();
            app.UseScheduleUI();
 
            app.UseSpecificationDocuments();
 
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks("/healthz");
            });
 
            app.UseComponent<ConsulApplicationComponent>(env);
 
            //lifetime.ApplicationStarted.Register(async () =>
            //{
            //    await ResourceUtils.BuildDynamicControllersAsync();
            //});
        }
    }
}