public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); //初始化autofac AutofacConfig.RegisterServices(); //新开一个线程轮训登录API PollingLoginService.Login(); } }
/// <summary> /// 轮询调用登录接口 /// 创建人: /// 创建时间:2019/3/11 15:51:06 /// </summary> public class PollingLoginService { /// <summary> /// 登录,每24小时轮询一次 /// </summary> public static void Login() { Task.Run(() => { LoginModel loginModel = new LoginModel { UserName = ConfigProvider.ApiLoginUserName, Password = ConfigProvider.ApiLoginPassword }; IApiLoginService apiLoginService = new ApiLoginService(); while (true) { var token = apiLoginService.Login(loginModel); ConfigProvider.LoginToken = token; Thread.Sleep(ConfigProvider.PollingLoginTime); } }); } }
很简单,在项目启动时,新开一个线程来处理这件事情。在任务里面循环去做。