在 ASP.NET 网站上安排作业,无需购买专用服务器

发布于 2024-07-16 22:57:21 字数 51 浏览 3 评论 0原文

如何在共享托管服务器上按照配置的计划时间执行各种任务(例如电子邮件警报/发送新闻信件)?

How can I perform various task (such as email alert/sending news letter) on a configured schedule time on a shared hosting server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‘画卷フ 2024-07-23 22:57:21

这是我过去用来执行此类操作的 Global.ascx.cs 文件,使用缓存过期来触发计划任务:

public class Global : HttpApplication
{
    private const string CACHE_ENTRY_KEY = "ServiceMimicCacheEntry";
    private const string CACHE_KEY = "ServiceMimicCache";

    private void Application_Start(object sender, EventArgs e)
    {
        Application[CACHE_KEY] = HttpContext.Current.Cache;
        RegisterCacheEntry();
    }

    private void RegisterCacheEntry()
    {
        Cache cache = (Cache)Application[CACHE_KEY];
        if (cache[CACHE_ENTRY_KEY] != null) return;
        cache.Add(CACHE_ENTRY_KEY, CACHE_ENTRY_KEY, null,
                  DateTime.MaxValue, TimeSpan.FromSeconds(120), CacheItemPriority.Normal,
                  new CacheItemRemovedCallback(CacheItemRemoved));
    }

    private void SpawnServiceActions()
    {
        ThreadStart threadStart = new ThreadStart(DoServiceActions);
        Thread thread = new Thread(threadStart);
        thread.Start();
    }

    private void DoServiceActions()
    {
        // do your scheduled stuff
    }

    private void CacheItemRemoved(string key, object value, CacheItemRemovedReason reason)
    {
        SpawnServiceActions();
        RegisterCacheEntry();
    }
}

目前,该文件每 2 分钟触发一次操作,但这可以在代码。

Here's a Global.ascx.cs file I've used to do this sort of thing in the past, using cache expiry to trigger the scheduled task:

public class Global : HttpApplication
{
    private const string CACHE_ENTRY_KEY = "ServiceMimicCacheEntry";
    private const string CACHE_KEY = "ServiceMimicCache";

    private void Application_Start(object sender, EventArgs e)
    {
        Application[CACHE_KEY] = HttpContext.Current.Cache;
        RegisterCacheEntry();
    }

    private void RegisterCacheEntry()
    {
        Cache cache = (Cache)Application[CACHE_KEY];
        if (cache[CACHE_ENTRY_KEY] != null) return;
        cache.Add(CACHE_ENTRY_KEY, CACHE_ENTRY_KEY, null,
                  DateTime.MaxValue, TimeSpan.FromSeconds(120), CacheItemPriority.Normal,
                  new CacheItemRemovedCallback(CacheItemRemoved));
    }

    private void SpawnServiceActions()
    {
        ThreadStart threadStart = new ThreadStart(DoServiceActions);
        Thread thread = new Thread(threadStart);
        thread.Start();
    }

    private void DoServiceActions()
    {
        // do your scheduled stuff
    }

    private void CacheItemRemoved(string key, object value, CacheItemRemovedReason reason)
    {
        SpawnServiceActions();
        RegisterCacheEntry();
    }
}

At the moment, this fires off your actions every 2 minutes, but this is configurable in the code.

不羁少年 2024-07-23 22:57:21

这里有人通过在 global.asax 中创建线程来做到这一点。 听起来他们在这方面取得了成功。 我自己从未测试过这种方法。

在我看来,这将是比重载缓存过期机制更好的选择。

Somone on here was doing this by creating threads in global.asax. Sounded like they were having success with it. I've never tested this approach myself.

This in my opinion would be a better option then overloading the cache expiration mechanism.

戴着白色围巾的女孩 2024-07-23 22:57:21

您可以在共享主机上使用ATrigger调度服务,没有任何问题
还可以使用 .Net 库来创建计划任务,而无需任何开销。

免责声明:我是 ATrigger 团队的一员。 这是一个免费软件,我没有任何商业目的。

You can use ATrigger scheduling service on a shared hosting without any problem.
A .Net library is also available to create scheduled tasks without overhead.

Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文