sunpengfei
2025-08-04 b9b4c926a7898d8b7d657e75ec83064390a5f9fb
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
using Furion;
using Furion.EventBus;
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.Core
{
    [AppStartup(2)]
    public class FlexJobApiCoreStartup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConsoleFormatter(options =>
            {
                options.WithTraceId = true;
                options.WithStackFrame = true;
            });
 
            services.AddFileLogging(options =>
            {
                options.WithTraceId = true;
                options.WithStackFrame = true;
                options.FileNameRule = fileName =>
                {
                    return string.Format(fileName, DateTime.UtcNow);    // 如果是本地时间使用 DateTime.Now
                };
            });
 
            services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
            {
                options.WithTraceId = true;
                options.WithStackFrame = true;
            });
 
            services.AddConfigurableOptions<AliyunOptions>();
 
            services.AddComponent<EventBusServiceComponent>();
 
            services.AddComponent<DistributedCacheServiceComponent>();
 
            services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(App.Assemblies.ToArray()));
 
            services.AddHostedService<XmlDocBuildHostedService>();
 
            services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
 
            services.AddCorsAccessor();
 
            services.AddSpecificationDocuments(options =>
            {
                options.OperationFilter<CustomOperationIdFilter>();
                options.SchemaFilter<EnumSchemaFilter>();
            });
 
            services.AddControllers()
                    .AddNewtonsoftJson(options =>
                    {
                        //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.UseSpecificationDocuments();
 
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
 
            lifetime.ApplicationStarted.Register(async () =>
            {
                await ResourceUtils.BuildWebApis();
            });
        }
    }
}