ningshuxia
2022-11-29 2f855d24150d407ae860885949ca700476c71438
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
using Furion;
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 Serilog;
using SqlSugar;
using System.Text.Json.Serialization;
using IStation.Application;
 
namespace IStation.WebApi 
{
    /// <summary>
    /// 
    /// </summary>
    [AppStartup(10)]
    public class Startup : AppStartup
    {
        /// <summary>
        /// 
        /// </summary>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConfigurableOptions<JWTSettingsOptions>();
 
            services.Configure<KestrelServerOptions>(options =>
            {
                options.Limits.MaxRequestBodySize = int.MaxValue;
            });
            services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = int.MaxValue;
            });
 
            if (Settings.WebApi.EnableJwt)
            {
                services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
            }
            
 
            services.AddCorsAccessor();
 
            services.AddControllers().AddInjectWithUnifyResult<XnRestfulResultProvider>();
            services.AddSwaggerGen(c =>
            {
                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 = Settings.SqlSugar.SnowFlakeWorkId;
        }
 
        /// <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, Settings.File.DataFolder);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //通过url访问文件
            app.UseStaticFiles(new StaticFileOptions()//自定义自己的文件路径
            {
                RequestPath = new PathString("/Data"),//对外的访问路径
                FileProvider = new PhysicalFileProvider(path)//指定实际物理路径
            }); 
 
            #endregion
 
            // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
            //app.UseSerilogRequestLogging();
 
            app.UseRouting();
 
            app.UseCorsAccessor();
 
            app.UseAuthentication();
            app.UseAuthorization();
 
            app.UseInject(string.Empty);
 
            app.UseEndpoints(endpoints =>
            {
                // 注册集线器
                endpoints.MapHub<ChatHub>("/hubs/chathub");
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
 
 
        }
 
 
 
 
    }
}