yl
2024-10-30 65a9633ebcf5cf4c0871d1c857a743ec7694c157
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using log4net;
using log4net.Config;
using log4net.Repository;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using VueWebCoreApi.ApiGroup;
using VueWebCoreApi.Extensions;
using VueWebCoreApi.Quartz;
using VueWebCoreApi.SignalR;
using VueWebCoreApi.Tools;
 
namespace VueWebCoreApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
 
        public IConfiguration Configuration { get; }
 
        //log4netÈÕÖ¾
        public static ILoggerRepository repository { get; set; }
        //¿çÓòÉèÖÃ
        readonly string ganweiCosr = "AllowSpecificOrigins";
 
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson(op =>
            {
                op.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
                op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                // ·µ»ØÊý¾ÝÊ××Öĸ²»Ð¡Ð´£¬CamelCasePropertyNamesContractResolverÊÇСд
                op.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });
            //½â¾öÎĵµÖÐÊ××Öĸ±»Ð¡Ð´µÄÎÊÌâ
            services.AddControllers().AddJsonOptions(config =>
            {
                config.JsonSerializerOptions.PropertyNamingPolicy = null;
            });
            #region Swagger µÚ¶þÖÖ·½Ê½
            services.AddSwaggerGen(c =>
            {
                typeof(OpenApiGroup).GetEnumNames().ToList().ForEach(version =>
                {
                    c.SwaggerDoc(version, new OpenApiInfo()
                    {
                        Title = $"Ì©·áµçÆ÷-MesApiÎĵµ:{version}",
                        Version = version,
                        Description = $"Panda.Sewerage :  {version}  ",
                    });
                });
 
                string basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                string xmlPath = Path.Combine(basePath, "VueWebCoreApi.xml");
                c.IncludeXmlComments(xmlPath, true);
            });
            #endregion
 
 
            // ÉèÖÃÅäÖÃ
            AppSetting.SetAppSetting(Configuration.GetSection("ConnectionStrings"));
            //log4netÅäÖÃ
            repository = LogManager.CreateRepository("AprilLog");
            XmlConfigurator.Configure(repository, new FileInfo("Config/log4net.config"));
            BasicConfigurator.Configure(repository);
            services.AddControllersWithViews();
            //Quartz
            services.AddHttpClient();
            services.AddHttpContextAccessor();
            services.AddSingleton<IPathProvider, PathProvider>();
            services.AddTransient<HttpResultfulJob>();
            services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
            services.AddSingleton<IJobFactory, IOCJobFactory>();
 
 
            //¿çÓòÉèÖÃ
            services.AddCors(options =>
            {
                options.AddPolicy(
                    "cors",
                    set =>
                    {
                        set.SetIsOriginAllowed(origin => true).AllowAnyHeader().AllowAnyMethod().AllowCredentials();
                    });
            });
            //ÉèÖÃSignalR
            services.AddSignalR();
 
 
            //ÉèÖÃsession,²¢ÉèÖó¬Ê±Ê±¼ä
            services.AddSession(o =>
             {
                 o.IdleTimeout = TimeSpan.FromSeconds(double.Parse(AppSetting.GetAppSetting("InProc")));
             });
            //ÉèÖÃCookies
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
 
            #region ÔÊÐí´óÎļþÉÏ´«
            //ÔÊÐíÉÏ´«´óÎļþ
            services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = 1073741824;//´Ë´¦ÏÞÖÆ×î´ó1G  
            });
            //½â¾öÎļþÉÏ´«Multipart body length limit 134217728 exceeded.
            services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = 1073741824; //´Ë´¦ÏÞÖÆ×î´ó1G  
            });
            #endregion ÔÊÐí´óÎļþÉÏ´«
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLeftTime, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            applicationLeftTime.ApplicationStarted.Register(() =>
            {
                DataOperator.SetConnectionString1(Convert.ToString(AppSetting.GetAppSetting("DBServer")));
            });
 
            #region Swagger·Ö×éµÚ¶þÖÖ
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                //ɾ³ýSwaggerÉÏĬÈÏSchemas¼Ü¹¹
                c.DefaultModelsExpandDepth(-1);
                //c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api Document");
                typeof(OpenApiGroup).GetEnumNames().ToList().ForEach(version =>
                {
                    c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{version}");
                });
 
            });
            #endregion
            app.UseHttpsRedirection();
 
            app.UseRouting();
 
            app.UseAuthorization();
            //Ìí¼Ó¾²Ì¬ÎļþÖмä¼þ
            app.UseDefaultFiles();
            //Ìí¼ÓÊÚȨÎļþ
            app.UseStaticFiles(new StaticFileOptions()
            {
                ContentTypeProvider = new FileExtensionContentTypeProvider()
                {
                    Mappings = {
                        [".xls"] = "application/vnd.ms-excel",
                        [".apk"] = "application/vnd.android.package-archive"
                    }
                }
            });
            app.UseStaticFiles();
            //¿çÓò
            app.UseCors("cors");
            //Óû§session·þÎñ
            app.UseSession();
            //cookies
            app.UseCookiePolicy();
            //HttpContext
            app.UseStaticHostEnviroment();
            //Quartz
            app.UseQuartz(env).UseStaticHttpContext();
            //ÅäÖÃExcelÏÂÔØ
            //app.UseStaticFiles(
            //    new StaticFileOptions
            //    {
            //      //ÉèÖò»ÏÞÖÆcontent-type 
            //       ServeUnknownFileTypes = true
            //    });
            //app.UseStaticFiles(new StaticFileOptions()
            //{
            //    ContentTypeProvider = new FileExtensionContentTypeProvider()
            //    {
            //        Mappings = { [".exe"] = "application/octect-stream" }
            //    }
            //});
 
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<ChatHub>("/ChatHub", options =>
                {
                    options.Transports =
                    HttpTransportType.WebSockets |
                    HttpTransportType.LongPolling;
                });
            });
            loggerFactory.AddLog4Net("Config/log4net.config");
        }
    }
}