首页 > 解决方案 > 在 IIS 上托管 ASP.NET 解决方案后启动系统计时器

问题描述

我创建了一个 MVC ASP.NET Standard(不是 .NET Core)解决方案并添加了一个 System.Timers.TimerGlobal.asax.cs我在注册服务之前启动了计时器。

托管后,即使我在Application_Start函数将启动的位置启用计时器并按如下方式挂钩计时器,计时器也不会启动,直到客户端调用网站:

// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(intervalInMiliseconds);
// Hook up the Elapsed event for the timer. 
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;

我的问题是,如果我想让计时器启动,无论是否有连接的用户,我应该在哪里启动计时器?我可以使用 ASP.NET Standard 还是应该使用 .NET Core?

我的场景是,我将创建一个托管 SignalR 集线器的 Windows 服务,并将定期向所有连接的用户发送消息。我想对 IIS 做同样的事情。

标签: c#asp.netiistimer

解决方案


推荐阅读