duheng
2025-02-07 c5136b7517998a076f1bd2e4abdda01decae8b6f
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using System.Text.Json.Serialization;
using IGeekFan.AspNetCore.Knife4jUI;
using Furion;
using Furion.SpecificationDocument;
using Microsoft.AspNetCore.Mvc.Controllers;
using Quartz;
 
namespace IStation.ChEr.WebApi
{
    /// <summary>
    ///
    /// </summary>
    [AppStartup(10)]
    public class Startup : AppStartup
    {
        /// <summary>
        ///
        /// </summary>
        public void ConfigureServices(IServiceCollection services)
        {
            // services.AddConfigurableOptions<JWT.JWTSettingsOptions>();
 
            services.Configure<KestrelServerOptions>(options =>
            {
                options.Limits.MaxRequestBodySize = int.MaxValue;
            });
            services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = int.MaxValue;
            });
 
            //检查 权限
            //services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
 
            services.AddCorsAccessor();
 
            services.AddControllers().AddInjectWithUnifyResult<XnRestfulResultProvider>();
            services.AddSwaggerGen(c =>
            {
                //支持knife4ui(必备)
                c.CustomOperationIds(s =>
                {
                    var a = s.ActionDescriptor as ControllerActionDescriptor;
                    return a.ControllerTypeInfo.Name + "-" + s.HttpMethod + a.ActionName;
                });
                c.UseInlineDefinitionsForEnums();
            }).AddMiniProfiler();
 
            services.AddJsonOptions(options =>
            {
                //返回属性大小写问题
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
                //返回时间格式
                options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter());
                options.JsonSerializerOptions.Converters.Add(new DateTimeNullableJsonConverter());
                options.JsonSerializerOptions.Converters.Add(new LongJsonConverter());
                //忽略循环引用
                options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; // 仅.NET 6支持
            });
            // 添加即时通讯
            //services.AddSignalR();
 
            // SnowFlakeSingle.WorkId = 2;// Settings.SqlSugarParasHelper.SqlSugar.SnowFlakeWorkId;
 
            IStation.LogHelper.Info("log4net initial OK");
 
            /*     //定时任务
                 services.AddQuartz(q =>
                 {
                     // Use a Scoped container to create jobs. I'll touch on this later
                     //q.UseMicrosoftDependencyInjectionScopedJobFactory();
 
                     var jobKey = new JobKey("CalcWaterPredict");
 
                     // Register the job with the DI container
                     q.AddJob<CalcWaterPredictJob>(opts => opts.WithIdentity(jobKey));
 
                     // Create a trigger for the job
                     q.AddTrigger(opts => opts
                         .ForJob(jobKey) // link to the CalcWaterPredict
                         .WithIdentity("CalcWaterPredict-trigger") // give the trigger a unique name
                         .WithCronSchedule("0 10 0/1 * * ? ")); // 每小时一次,每次在该小时的06分开始执行:
                 });*/
 
            // Add the Quartz.NET hosted service
            //  services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
        }
 
        /// <summary>
        ///
        /// </summary>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
 
            // 添加规范化结果状态码,需要在这里注册
            app.UseUnifyResultStatusCodes();
 
            app.UseHttpsRedirection();
 
            #region 开放 data 文件夹(可以通过url访问文件)
 
            //app.UseStaticFiles();
            string path = AppContext.BaseDirectory;
            path = Path.Combine(path, "Data");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //通过url访问文件
            app.UseStaticFiles(new StaticFileOptions()//自定义自己的文件路径
            {
                RequestPath = new PathString(""),//对外的访问路径 之前为 "/Data" ,改动是为了隐藏Data对外显示
                FileProvider = new PhysicalFileProvider(path)//指定实际物理路径
            });
 
            #endregion 开放 data 文件夹(可以通过url访问文件)
 
            // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
            //app.UseSerilogRequestLogging();
 
            app.UseRouting();
 
            app.UseCorsAccessor();
 
            app.UseAuthentication();
            app.UseAuthorization();
 
            // 配置Swagger-Knife4UI(路由前缀一致代表独立,不同则代表共存)
            app.UseKnife4UI(options =>
            {
                options.RoutePrefix = "Api";  // 配置 Knife4UI 路由地址
                foreach (var groupInfo in SpecificationDocumentBuilder.GetOpenApiGroups())
                {
                    options.SwaggerEndpoint("/" + groupInfo.RouteTemplate, groupInfo.Title);
                }
            });
 
            app.UseInject(string.Empty);
 
            app.UseEndpoints(endpoints =>
            {
                // 注册集线器
                //endpoints.MapHub<ChatHub>("/hubs/chathub");
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}