Startup.cs
14.9 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using Autofac.Extensions.DependencyInjection;
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Hh.Mes.Service;
using Hh.Mes.Service.Repository;
using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Linq;
using Quartz;
using Quartz.AspNetCore;
using Quartz.AspNetCore.Logging;
using Quartz.Job.Jobs;
using Quartz.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using WebMvc.Aop;
namespace WebMvc
{
public class Startup
{
public IConfiguration Configuration { get; }
public IWebHostEnvironment webHostEnvironment { get; set; }
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
webHostEnvironment = env;
}
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
#region 自定义配置appsettings.json 节点 AppSettings.SetAppSetting(Configuration.GetSection("AppCustomSettings"));
AppSettings.SetAppSetting(Configuration.GetSection("AppCustomSettings"));
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend1 = AppSettings.GetAppSeting("AppCustomExtend1");
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend2 = AppSettings.GetAppSeting("AppCustomExtend2");
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend3 = AppSettings.GetAppSeting("AppCustomExtend3");
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend4 = AppSettings.GetAppSeting("AppCustomExtend4");
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend5 = AppSettings.GetAppSeting("AppCustomExtend5");
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend6 = AppSettings.GetAppSeting("AppCustomExtend6");
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend7 = AppSettings.GetAppSeting("AppCustomExtend7");
ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend8 = AppSettings.GetAppSeting("AppCustomExtend8");
#endregion
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
options.ValueLengthLimit = int.MaxValue;
options.KeyLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = int.MaxValue;
options.MultipartBoundaryLengthLimit = int.MaxValue;
//解决文件上传Request body too large
options.MultipartBodyLengthLimit = 268435456;
});
services.AddControllersWithViews(option =>
{
option.EnableEndpointRouting = false;
option.ModelBinderProviders.Insert(0, new JsonBinderProvider());
//加入全局异常类
option.Filters.Add<HttpGlobalExceptionFilter>();
});
//Asp.Net Core获取请求上下文HttpContext https://www.cnblogs.com/tianma3798/p/10361644.html
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
//内存缓存
services.AddDistributedMemoryCache();
//Redis缓存
//services.AddStackExchangeRedisCache(options =>
//{
// options.Configuration = Configuration.GetConnectionString("RedisIp");
// options.InstanceName = "SampleInstance";
//});
services.AddOptions();
#region HTTPS
//services.AddMvc(options =>
//{
// options.SslPort = ConfigRead.GetInstance.GetAppsetConnection().WebPort;
// options.Filters.Add(new RequireHttpsAttribute());
//});
//services.AddAntiforgery(
// options =>
// {
// options.Cookie.Name = "_af";
// options.Cookie.HttpOnly = true;
// options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
// options.HeaderName = "X-XSRF-TOKEN";
// });
#endregion
//数据库连接地址
var connectionString = ConfigRead.GetInstance.GetAppsetConnection().BaseDBContext;
#region 注册Quartz中间件
services.AddQuartz(options =>
{
options.UseSqlServer(connectionString);
options.UseProperties(false);
});
#endregion
#region 注册SqlSugars、操作日志、XSS攻击防御等
//SqlSugars
services.BatchRegisterService(Program.service, Program.serviceSuffix);
services.AddDirectoryBrowser();
//操作日志
services.AddScoped<OperLogFilter>();
//services.AddScoped<InterfaceLogFilter>();
//xss攻击防御
services.AddScoped<XSSFilterAttribute>();
#endregion
#region 注入登录、授权等服务
services.AddScoped<LoginParse>();
services.AddScoped<AuthContextFactory>();
services.AddScoped<SystemAuthStrategy>();
services.AddScoped<NormalAuthStrategy>();
services.AddScoped(typeof(IAuth), typeof(LocalAuth));
#endregion
//解决开发环境修改html,需要重新编译启动程序,编译发布环境不需要 https://www.cnblogs.com/Can-daydayup/p/13630050.html#!comments
if (webHostEnvironment.IsDevelopment()) services.AddRazorPages().AddRazorRuntimeCompilation();
//使用AutoFac进行注入
var serviceProvider = new AutofacServiceProvider(AutofacExt.InitAutofac(services));
//Redis 设置缓存
Task.Factory.StartNew(() =>
{
var baseInfoCacheService = serviceProvider.GetService<BaseInfoCacheService>();
baseInfoCacheService.SetBaseInfoCacheFirst();
});
return serviceProvider;
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
ConfigRead.GetInstance.GetAppsetConnection().IsDevelopment = false;
app.UseDeveloperExceptionPage();
}
else
{
if (IsExtSysFile()) return;
ConfigRead.GetInstance.GetAppsetConnection().IsDevelopment = true;
app.UseExceptionHandler("/htmlTemp");
}
app.UseStaticFiles();
Console.ResetColor();
#region apk https://www.cnblogs.com/1175429393wljblog/p/8624679.html
app.UseStaticFiles(
new StaticFileOptions
{
ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
{
{ ".apk", "application/vnd.android.package-archive" }
})
});
#endregion
#region 启用Quartz中间件 开发环境不启用定时器,如果需要开启 isOpenForDev 设置true ,发布时默认值false;
var isOpenForDev = false;
var isDevelopment = ConfigRead.GetInstance.GetAppsetConnection().IsDevelopment;
if (isDevelopment)
{
InitialQuartzAndData(app, true);
}
else
{
InitialQuartzAndData(app, isOpenForDev);
}
Console.WriteLine();
#endregion
app.UseMvcWithDefaultRoute();
#region http 500
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = 500;
// 检查请求是否为 Ajax 请求
if (context.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync($@"{env.WebRootPath}/htmlTemp/500.html");
}
});
});
app.UseStatusCodePagesWithReExecute("/htmlTemp/{0}");
#endregion
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
}
#region 自定义方法
private bool IsExtSysFile()
{
var webMvcPath = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "WebMvc.xml");
if (!File.Exists(webMvcPath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("WebMvc.xml 文件路径不存在,请在bin/Debug 手动拷贝复制该文件到发布目录下面(和appsettings.json平级)!:" + webMvcPath);
Log4NetHelper.Instance.Info("WebMvc path:" + webMvcPath);
Console.ReadLine();
return true;
}
var wwwRootPath = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "wwwroot");
if (!Directory.Exists(wwwRootPath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("wwwroot 文件夹不存在,请手动拷贝复制该文件到发布目录下面(和appsettings.json平级)!:" + webMvcPath);
Log4NetHelper.Instance.Info("wwwroot path:" + wwwRootPath);
Console.ReadLine();
return true;
}
var aaptPath = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "tools", "aapt.exe");
if (!File.Exists(aaptPath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("aapt.exe 文件路径不存在,请在根目录tools/aapt.exe手动拷贝复制该文件到发布目录下面(和appsettings.json平级)!:" + aaptPath);
Log4NetHelper.Instance.Info("aaptPath path:" + aaptPath);
Console.ReadLine();
return true;
}
var unzipPath = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "tools", "unzip.exe");
if (!File.Exists(unzipPath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("unzip.exe 文件路径不存在,请在根目录tools/unzip.exe手动拷贝复制该文件到发布目录下面(和appsettings.json平级)!:" + aaptPath);
Log4NetHelper.Instance.Info("unzip path:" + unzipPath);
Console.ReadLine();
return true;
}
return false;
}
#region 初始化Quartz组件,添加定时任务数据。解决服务程序重启后定时任务无法执行问题。
/// <summary>
/// </summary>
/// <param name="builder"></param>
/// <param name="isDevelopment"> true是生产环境,false是开发环境</param>
/// <param name="isOpen">true开启定时器,有时候开发环境需要测试,默认是false</param>
/// <returns></returns>
public static IApplicationBuilder InitialQuartzAndData(IApplicationBuilder builder, bool isDevelopment, bool isOpen = false)
{
LogProvider.SetCurrentLogProvider(builder.ApplicationServices.GetRequiredService<LoggingProvider>());
var sched = builder.ApplicationServices.GetRequiredService<IScheduler>();
var sysJobRepository = new SysJobRepository();
var sysJobs = sysJobRepository.GetList(x => x.status == "0");
foreach (var item in sysJobs)
{
try
{
var jobDataMap = CreateJobDataMap(item.methodParams);
var classType = LoadJobClass(item.methodName);
if (classType != null)
{
IJobDetail job = JobBuilder.Create(classType)
.WithIdentity(item.jobName)
.UsingJobData(jobDataMap)
.Build();
// 创建并调度触发器
var trigger = TriggerBuilder.Create()
.WithIdentity(item.jobName)
.WithCronSchedule(item.cronExpression)
.Build();
// 删除旧任务并调度新任务
sched.DeleteJob(new JobKey(item.jobName)).Wait();
sched.ScheduleJob(job, trigger).Wait();
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"创建定时任务[{item.jobName}]异常:{ex.Message}");
Console.ResetColor();
}
}
// 启动调度器
if (isOpen || isDevelopment)
{
sched.Start();
}
// 输出提示信息
LogSchedulerStatus(isDevelopment, isOpen);
return builder;
}
private static JobDataMap CreateJobDataMap(string methodParams)
{
var jobDataMap = new JobDataMap();
var json = JObject.Parse(methodParams);
foreach (var v in json)
{
jobDataMap.Add(v.Key, v.Value.ToString());
}
return jobDataMap;
}
private static Type LoadJobClass(string methodName)
{
return Assembly.Load(typeof(JobBase).Assembly.FullName).GetType($"{typeof(JobBase).Namespace}.{methodName}");
}
private static void LogSchedulerStatus(bool isDevelopment, bool isOpen)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
if (isDevelopment)
{
Console.WriteLine("友好提示: webApp线上环境已开启定时器作业任务!");
}
else
{
if (isOpen)
{
Console.WriteLine("友好提示: webApp线下环境已开启定时器作业任务!");
}
else
{
Console.WriteLine("友好提示: 基础版本扩展新项目后需要重新新建数据库");
Console.WriteLine("友好提示: webApp线下环境未开启定时器作业任务!");
}
}
Console.ResetColor();
}
#endregion
#endregion
}
}