Commit 27f3b25e authored by liudong1993's avatar liudong1993

1

parent ef1fbd79
......@@ -34,11 +34,7 @@ namespace EduSpider.WebApi
//ActoFac注入
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
List<string> corsArray = new()
{
"http://localhost:7100",
"http://localhost:7200",
};
List<string> corsArray = GetCorsArray();
services.AddCors(options => options.AddPolicy("AllowCors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(corsArray.ToArray())));
services.AddControllers();
services.AddMvc(options =>
......@@ -56,8 +52,27 @@ namespace EduSpider.WebApi
}
/// <summary>
/// 获取跨域配置
/// </summary>
/// <returns></returns>
public List<string> GetCorsArray()
{
List<string> corsArray = new()
{
"http://localhost:7100",
"http://localhost:7200",
};
var ArrayStr = ConfigHelper.GetAppsettings("corsArray");
if (!string.IsNullOrWhiteSpace(ArrayStr))
{
corsArray.AddRange(JsonHelper.Deserialize<List<string>>(ArrayStr));
}
return corsArray;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
{
if (env.IsDevelopment())
{
......@@ -75,6 +90,16 @@ namespace EduSpider.WebApi
{
endpoints.MapControllers();
});
appLifetime.ApplicationStarted.Register(() =>
{
Timers.TimerJobj.RunTimer(); //网站启动完成执行
});
appLifetime.ApplicationStopped.Register(() =>
{
Timers.TimerJobj.RunStop(); //网站停止完成执行
});
}
}
}
using System;
using System.IO;
using System.Threading;
using VTX.FW.Helper;
namespace EduSpider.WebApi.Timers
{
/// <summary>
/// 定时任务
/// </summary>
public class TimerJobj
{
static System.Timers.Timer timer1;//计时器
public static void RunTimer()
{
timer1 = new System.Timers.Timer
{
Interval = (1000 * 60) * (60 * 3) //3小时执行一次
};
timer1.Elapsed += new System.Timers.ElapsedEventHandler(ClearFile);
timer1.Enabled = true;
}
public static void RunStop()
{
timer1.Enabled = false;
}
/// <summary>
/// 防止重复提交
/// </summary>
private static int inTimer = 0;
/// <summary>
/// 清理文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void ClearFile(object sender, System.Timers.ElapsedEventArgs e)
{
if (Interlocked.Exchange(ref inTimer, 1) == 0)
{
string rootBook = AppDomain.CurrentDomain.BaseDirectory;
try
{
DirectoryInfo dir = new(rootBook + "/upfile/temporary");
if (dir != null && dir.Exists)
{
DirectoryInfo[] dirArr = dir.GetDirectories();
foreach (DirectoryInfo item in dirArr)
{
if (item.CreationTime < DateTime.Now.AddDays(-1))
item.Delete(true);
}
foreach (FileInfo fi in dir.GetFiles())
{
if (fi.CreationTime < DateTime.Now.AddDays(-1))
fi.Delete();
}
}
}
catch
{
LogHelper.WriteInfo("ClearFile", "清理临时文件失败_ClearFile");
}
Interlocked.Exchange(ref inTimer, 0);
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment